#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const long long MOD = 1000000007;
const long long INF = 9999999999999999;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll A[3020][5];
ll N;

bool isOK(ll key){
  vector<ll> aup(32);
  rep(i,N){
    ll temp = 0;
    rep(k,5){
      if(A[i][k]>=key){
        temp+=pow(2,k);
      }
    }
    aup[temp]++;
  }
  bool aru = 0;
  rep(i,32){
    rep(k,32){
      rep(t,32){
        if((i|k|t) == 31){
          if(aup[i]>0&&aup[k]>0&&aup[t]>0){
            //cout << i << k << t << endl;
            aru = 1;
          }
        }
      }
    }
  }
  return aru;
}
            
        
    


int main(){
  cin>>N;
  rep(i,N){
    cin>>A[i][0]>>A[i][1]>>A[i][2]>>A[i][3]>>A[i][4];
  }
  ll right = MOD;
  ll left = -1;
  while((right-left)>1){
    ll mid=left+(right-left)/2;
    if(isOK(mid)){
      left = mid;
    }
    else{
      right = mid;
    }
  }
  cout << left << endl;
  return 0;
}
#include<algorithm>
#include<iostream>
using namespace std;
#define ll long long
#define For(i,l,r) for(int i=l;i<=r;i++)
#define MAXN 200001
int N,ans;ll now=0;struct A{int a,b;bool operator<(A x)const{return 2ll*a+b>2ll*x.a+x.b;}}a[MAXN];
int main()
{
	cin>>N;For(i,1,N)cin>>a[i].a>>a[i].b,now-=a[i].a;sort(a+1,a+N+1);
	For(i,1,N){if(now>0)break;now+=2ll*a[i].a+a[i].b,ans++;}cout<<ans<<endl;return 0;
}