#include <bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define inf 1000000000000000001
#define mod 1000000007
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
#define fast_cin ios_base::sync_with_stdio(false);cin.tie(NULL);
#define precise fixed(cout);cout<<setprecision(16);
#define Set(N,p) N=((N)|((1LL)<<(p)))
#define Reset(N,p) N=((N)&(~((1LL)<<(p))))
#define Check(N,p) (!(((N)&((1LL)<<(p)))==(0)))
#define POPCOUNT __builtin_popcountll
#define RIGHTMOST __builtin_ctzll
#define LEFTMOST(x) (63-__builtin_clzll((x)))
#define NUMDIGIT(x,y) (((vlong)(log10((x))/log10((y))))+1)
#define OUT(x) for(auto a:x) cout << a << " "; cout << endl;
#define OK cout << "@===================ok===================@" <<endl;
#define WTF cout <<"< "<<lo<<" | "<< hi <<" >" << endl;

using namespace std;
vll temp;

void prec(){
    ll x=1;
    while(x<=1e16){
        x*=1000;
        temp.pb(x);
    }
}
int main()
{
    prec();
    ll n;
    cin >> n;
    ll res=0;
    for(auto a:temp){
        if(a>n) break;
        ll dist=n-a+1;
        res+=dist;
    }
    cout << res <<"\n";
}

//雪花飄飄北風嘯嘯
//天地一片蒼茫

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl

#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound

#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug

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

int n;
int arr[3005][5];

int val[3005];

int memo[3005][32][4];

int dp(int i,int j,int k){
	if (k==4) return 0;
	if (i==n) return j==31;
	if (memo[i][j][k]!=-1) return memo[i][j][k];
	
	return memo[i][j][k]=max(dp(i+1,j,k),dp(i+1,j|val[i],k+1));
}

bool can(int i){
	rep(x,0,n){
		val[x]=0;
		rep(y,0,5) if (arr[x][y]>=i) val[x]|=(1<<y);
	}
	
	//cout<<i<<endl;
	//rep(x,0,n) cout<<val[x]<<" "; cout<<endl;
	
	memset(memo,-1,sizeof(memo));
	return dp(0,0,0);
}


int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);
	
	cin>>n;
	rep(x,0,n) rep(y,0,5) cin>>arr[x][y];
	
	int lo=0,hi=1e9+5,mi;
	while (hi-lo>1){
		mi=hi+lo>>1;
		
		if (can(mi)) lo=mi;
		else hi=mi;
	}
	
	cout<<lo<<endl;
}
