#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using P = pair<int, int>;
#define all(x) x.begin(), x.end()
#define rep(x,s,e) for (auto x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e)?x++:x--))
#define sz(x) (int)x.size()
const char nl = '\n';
const int inf = 2e5*1e9;
const int N = 1e5+5e4+100;
int A[N][3];
bool X(int x, int y) {
return A[x][0] > A[y][0];
}
bool Y(int x, int y) {
return A[x][1] > A[y][1];
}
bool Z(int x, int y) {
return A[x][2] > A[y][2];
}
void solve() {
int n; cin >> n;
rep(i, 0, n)cin >> A[i][0] >> A[i][1] >> A[i][2];
vector<int> a(n), b(n), c(n);
rep(i, 0, n)a[i] = b[i] = c[i] = i;
sort(all(a), X);
sort(all(b), Y);
sort(all(c), Z);
//for (auto i: a)cout << i << " ";
//cout << nl;
//for (auto i: b)cout << i << " ";
//cout << nl;
//for (auto i: c)cout << i << " ";
//cout << nl;
vector<int> vis(n);
int x = 0, y = 0, z = 0;
while (x < n && y < n && z < n) {
int bir = a[x], iki = b[y], uc = c[z];
//cout << bir << " " << iki << " " << uc << nl;
bool ok = true;
if (vis[bir])x += 1, ok = false;
if (vis[iki])y += 1, ok = false;
if (vis[uc])z += 1, ok = false;
if (!ok)continue;
int c1 = (A[bir][1] >= A[iki][1])+(A[bir][2] >= A[uc][2]);
int c2 = (A[iki][0] >= A[bir][0])+(A[iki][2] >= A[uc][2]);
int c3 = (A[uc][0] >= A[bir][0])+(A[uc][1] >= A[iki][1]);
if (c1 > 0)x += 1, ok = false, vis[bir] = 1;
if (c2 > 0)y += 1, ok = false, vis[iki] = 1;
if (c3 > 0)z += 1, ok = false, vis[uc] = 1;
if (ok) {cout<< A[bir][0]+A[iki][1]+A[uc][2] << nl; return;}
}
cout << -1 << nl;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
//int t; cin >> t;
//while (t--)
solve();
return 0;
}