#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
bool fc=true;
vector<vector<pair<int,int> > > vec(3,vector<pair<int,int>>(n));
vector<vector<int> > mass(n,vector<int>(3));
vector<int> inds(3);
for(int i=0;i<n;++i)
for(int j=0;j<3;++j){
cin>>mass[i][j];
vec[j][i]={mass[i][j],i};
}
for(int i=0;i<3;++i)
sort(vec[i].rbegin(),vec[i].rend());
while(fc){
fc=false;
for(int i=0;i<3;++i)
for(int j=0;j<3;++j){ //haysyny ashaga cokduryas
if(i==j)
continue;
while(inds[j]<n && vec[i][inds[i]].first<=mass[vec[j][inds[j]].second][i]){
inds[j]++;
fc=true;
}
if(inds[j]==n){
cout<<-1;
return 0;
}
}
}
cout<<vec[0][inds[0]].first+vec[1][inds[1]].first+vec[2][inds[2]].first;
}