#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define ff first
#define ss second
#define SZ(s) (int)s.size()
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n;
cin >> n;
vector <ll> x(n), y(n), z(n);
priority_queue <pair <ll, int>> q[3];
for(int i = 0; i < n; i++) {
cin >> x[i] >> y[i] >> z[i];
q[0].push({x[i], i});
q[1].push({y[i], i});
q[2].push({z[i], i});
}
vector <int> del(n, 0);
while(true) {
for(int i = 0; i < 3; i++) {
while(SZ(q[i]) and del[q[i].top().ss]) q[i].pop();
}
if(!SZ(q[0]) or !SZ(q[1]) or !SZ(q[2])) {
cout << -1 << '\n';
return 0;
}
int a = q[0].top().ss;
int b = q[1].top().ss;
int c = q[2].top().ss;
vector <int> bad(n, 0);
if(y[a] == y[b] or z[a] == z[c]) bad[a] = 1;
if(x[b] == x[a] or z[b] == z[c]) bad[b] = 1;
if(x[c] == x[a] or y[c] == y[b]) bad[c] = 1;
if(!bad[a] and !bad[b] and !bad[c]) {
cout << x[a] + y[b] + z[c] << '\n';
return 0;
}
if(bad[a]) del[a] = 1;
if(bad[b]) del[b] = 1;
if(bad[c]) del[c] = 1;
}
return 0;
}