#include<bits/stdc++.h>
using namespace std;
template<typename T>inline bool read(T&x){
x=0;char c=getchar();int f=1;while(!isdigit(c)&&(c!='-')&&(c!=EOF))c=getchar();
if(c==EOF)return 0;if(c=='-')f=-1,c=getchar();
while(isdigit(c)){x=x*10+(c&15);c=getchar();}x*=f;return 1;
}template<typename T,typename...Args>inline bool read(T&x,Args&...args){bool res=1;res&=read(x);res&=read(args...);return res;}
typedef long long LL;typedef unsigned long long u64;typedef unsigned u32;typedef long double LD;
#define il inline
#define pln putchar('\n')
#define For(i,a,b)  for(int i=(a),(i##i)=(b);i<=(i##i);++i)
#define Rep(i,n)    for(int i=0,(i##i)=(n);i<(i##i);++i)
#define Fodn(i,a,b) for(int i=(a),(i##i)=(b);i>=(i##i);--i)
const int M=1000000007,INF=0x3f3f3f3f;const long long INFLL=0x3f3f3f3f3f3f3f3fLL;
const int N=100007;

int n,a[N][7],ma;

il bool f(int x){
	bool g[37];
	memset(g,0,sizeof g);
	For(i,1,n){
		u32 u=0;
		For(j,1,5)if(a[i][j]>=x)u|=(1<<(j-1));
		g[u]=1;
	}
	for(u32 i=0;i<=31;++i)if(g[i])
	for(u32 j=i;j<=31;++j)if(g[j]){
		u32 u=(i|j)^31u;
		if(u==0)return 1;
		Rep(s,32)if(g[s]&&((s&u)==u))return 1;
	}
	return 0;
}

inline void init(){
	read(n);For(i,1,n)For(j,1,5)read(a[i][j]),ma=max(a[i][j],ma);
}
inline void solve(){
	LL l=0,r=ma;
	while(l<r){
		LL mid=(l+r+1)/2;
		if(f(mid))l=mid;else r=mid-1;
	}
	printf("%lld\n",l);
}
signed main(){
	init();solve();
	return 0;
}
//                        YATIN KWATRA

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize ("Ofast")


#define ll                      long long
#define ar                      array
#define sz(v)                   (int)(v.size())
#define inf                     1e18
#define int                     ll
#define FIO                     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ld                      long double
#define ull                     unsigned long long
#define endl                    "\n"
#define fo(i,a,b)               for(int i = a; i<=b ; i++)
#define rfo(i,a,b)              for(int i = a; i>=b ; i--)
#define vii                     vector<int>
#define pq                      priority_queue
#define uomii                   unordered_map<int,int,best_hash>
#define all(v)                  v.begin(),v.end()
#define mp                      make_pair
#define pb                      push_back
#define pob                     pop_back
#define ff                      first
#define ss                      second
#define pii                     pair<int,int>
#define mii                     map<int,int>
#define vvii                    vector<vii>
#define mod                     1000000007
#define MIN                     -1e9
#define pi                      3.1415926535897932384626433832795
#define cz(x)                   63 - __builtin_clzll(x)

using namespace std;
using namespace __gnu_pbds;
template<class T> using oset =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;


struct best_hash {
	static uint64_t splitmix64(uint64_t x) {

		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}

	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};

const int N = 3e3 + 1;

int dp[N][N];
int v[N];
vii has[N][N]; // pos for remainder j from after i
int n;

int ways(int pos, int k) {
	if (pos == n + 1) return 1;
	int &ans = dp[pos][k];
	if (ans != -1) return ans;
	ans = 0;

	// now go to next k+1 or k

	int rem = v[pos - 1] % k;

	int idx = lower_bound(all(has[k][rem]), pos) - has[k][rem].begin();
	if (idx < sz(has[k][rem])) {
		int to = has[k][rem][idx];
		if (to < n) ans = (ways(to + 1, k) + ways(to + 1, k + 1)) % mod;
		else ans = ways(to + 1, k);
	}
	return ans;
}

void solve()
{
	cin >> n;
	fo(i, 1, n) cin >> v[i];

	memset(dp, -1, sizeof(dp));

	fo(i, 1, n) {
		int sum = 0;
		fo(j, 1, n) {
			sum += v[j];
			has[i][sum % i].pb(j);
		}
	}

	fo(i, 1, n) v[i] += v[i - 1];
	cout << ways(1, 1);
}

signed main() {

	FIO
	int t;
	t = 1;
	//cin >> t;
	fo(i, 0, t - 1) {
		solve();
	}
	return 0;
}
