#include<bits/stdc++.h>
using namespace std;

int n,val[200005][10],init[10];
bool life[35][6];

bool ok(int x)
{
	for(int i=1;i<init[5];i++)
		for(int j=1;j<=3;j++)
			life[i][j]=0;
	int now;
	for(int i=1;i<=n;i++)
	{
		now=0;
		for(int j=0;j<5;j++)
		{
			if(val[i][j]>=x)
				now+=init[j];
		}
		life[now][1]=1;
		for(int j=0;j<init[5];j++)
			for(int k=1;k<=2;k++)
				life[j|now][k+1]|=life[j][k];
	}
	return life[init[5]-1][3];
}

int main()
{
	init[0]=1;
	for(int i=1;i<=5;i++)
		init[i]=init[i-1]*2;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		for(int j=0;j<5;j++)
			scanf("%d",&val[i][j]);
	for(int i=n+1;i<=200000;i++)
		for(int j=0;j<5;j++)
			val[i][j]=val[i%n+1][j];
	n=200000; 
	int l=1,r=1e9,mid;
	life[0][1]=life[0][2]=life[0][3]=1; 
	while(l!=r)
	{
		mid=(l+r+1)/2;
		if(ok(mid))
			l=mid;
		else
			r=mid-1;
	}
	printf("%d\n",l);
	return 0;
}
// Problem : D - Choose Me
// Contest : AtCoder - AtCoder Beginner Contest 187
// URL : https://atcoder.jp/contests/abc187/tasks/abc187_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

//HAR HAR MAHADEV 
//WHEN IT GETS HARDER REMEMBER WHY YOU STARTED💪 
/*ヽ｀、ヽ｀｀、ヽ｀ヽ｀、、ヽ ｀ヽ 、ヽ｀🌙｀ヽヽ｀ヽ、ヽ｀
ヽ｀、ヽ｀｀、ヽ 、｀｀、 ｀、ヽ｀ 、｀ ヽ｀ヽ、ヽ ｀、ヽ｀｀、
ヽ、｀｀、｀、ヽ｀｀、 、ヽヽ｀、｀、、ヽヽ、｀｀、 、 ヽ｀、
ヽ｀｀、 ヽ｀ヽ｀、、ヽ ｀ヽ 、 🚶ヽ｀｀｀｀ヽヽヽ｀、、ヽ｀、、ヽ*/
#include <bits/stdc++.h> 
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
 
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
 
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long   ll;
typedef long double ld;
typedef complex< ld > cd;
 
typedef pair<int, int> pi;
typedef pair<ll, ll>   pl;
typedef pair<ld, ld>   pd;
 
typedef vector< int > vi;
typedef vector< ld >  vd;
typedef vector< ll >  vl;
typedef vector< pi >  vpi;
typedef vector< pl >  vpl;
typedef vector< cd >  vcd;

#define l00p(i, a, b) for (int i=a; i<(b); i++)
#define loop(i, a)    for (int i=0; i<(a); i++)
#define rep1(i, a, b) for (int i = (b)-1; i >= a; i--)
#define rep(i, a)     for (int i = (a)-1; i >= 0; i--)
#define trav(a, x)    for (auto& a : x)
#define uid(a, b)     uniform_int_distribution<int>(a, b)(rng)
#define vt vector
#define bug(x) cout<<#x<<"="<<x<<endl;

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f  first
#define s  second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ins insert
//#define MOD 998244353
 
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<class T> using PR = pair<T,T>;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ld pii=3.14159265359; 
const int MOD = 1000000007;
const char cl = '\n';
const ll INF = 1e18;
const int MX = 100001; //check the limits, dummy
constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set
void solve(){
	int n;
	cin>>n;
	vt<pair<ll,pl>> votes(n);
	ll suma=0;
	loop(i,n){
		cin>>votes[i].s.f>>votes[i].s.s;
		suma+=votes[i].s.f;
		votes[i].f=2*votes[i].s.f+votes[i].s.s;
	}
	sort(rall(votes));
	ll sumb=0;
	int cnt=0;
	loop(i,n){
		suma-=votes[i].f;
		cnt++;
		if(suma<0){
			cout<<cnt<<cl;
			return;
		}
	}
	return;
}
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); 
	int t=1;
	while(t--){
		solve();
	}
	return 0;
}
/* stuff you should look for
	* jab answer dhoondhna ho to matlb binary search beta
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/
