#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, long long> pdl;
const long double pi = 3.141592653589793;
#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for(int i = 0;i < n;i++)
#define pb push_back
#define mp make_pair
#define mod 1000000007
ll power(ll a, ll b) {
if (b == 0) return 1;
ll pp = power(a, b/2);
pp = (pp*pp) % mod;
if(b%2 == 0) return pp;
return (pp*a)%mod;
}
ll A[1000][10];
ll solved[10];
ll n;
vector<vector<ll> > dp;
ll help(ll sby, ll i) {
long double z = sby*1.0/(n+i);
if(z > 1.0/2) {
return 2*sby-n-1;
}
if(z > 1.0/4) {
return 4*sby-n-1;
}
if(z > 1.0/8) {
return 8*sby-n-1;
}
if(z > 1.0/16) {
return 16*sby-n-1;
}
if(z > 1.0/32) {
return 32*sby-n-1;
}
return mod;
}
void qupdate(ll sby) {
// solved by sby out of n
vector<ll> B;
ll i = 0;
ll j = help(sby, i);
while (j <= mod) {
B.pb(j);
//cout<<i<<" "<<j<<endl;
if(j == mod) break;
i = j+1;
j = help(sby, i);
}
if(B.size() > 0) dp.pb(B);
}
ll points(ll sby, ll i) {
long double z = sby*1.0/(n+i);
if(z > 1.0/2) {
return 500;
}
if(z > 1.0/4) {
return 1000;
}
if(z > 1.0/8) {
return 1500;
}
if(z > 1.0/16) {
return 2000;
}
if(z > 1.0/32) {
return 2500;
}
return 3000;
}
ll playerA(ll i) {
ll score = 0;
rep(j, 5) {
if(A[0][j] != -1) {
ll z;
if(A[1][j] == -1 || A[0][j] < A[1][j])
z = points(solved[j], i);
else z = points(solved[j]+i, i);
score += (z/250)*(250-A[0][j]);
}
}
return score;
}
ll playerB(ll i) {
ll score = 0;
rep(j, 5) {
if(A[1][j] != -1) {
ll z;
if(A[0][j] == -1 || A[0][j] < A[1][j])
z = points(solved[j], i);
else z = points(solved[j]+i, i);
score += (z/250)*(250-A[1][j]);
}
}
return score;
}
int main() {
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
cin>>n;
rep(i, n) {
rep(j, 5) cin>>A[i][j];
}
rep(i, 5) {
ll counter = 0;
rep(j, n) if(A[j][i] != -1) counter++;
solved[i] = counter;
}
rep(i, 5) {
if(A[0][i] == -1 and A[1][i] != -1) {
qupdate(solved[i]);
}
}
vector<ll> C(1, -1);
vector<int> tex(dp.size(), 0);
while (true) {
ll minm = LONG_LONG_MAX;
rep(i, dp.size()) {
if(tex[i] < dp[i].size()) minm = min(minm, dp[i][tex[i]]);
}
if(minm == LONG_LONG_MAX) break;
C.pb(minm);
rep(i, dp.size()) {
if(tex[i] < dp[i].size() and dp[i][tex[i]] == minm) tex[i]++;
}
}
if(C.size() == 1) C.pb(mod);
//rep(i, C.size()) cout<<C[i]<<" ";cout<<endl;
for(int i = 1;i < C.size();i++) {
ll s = C[i-1]+1;
ll e = C[i];
ll mid = (s+e)/2;
ll minm = LONG_LONG_MAX;
while (s <= e) {
ll sA = playerA(mid);
ll sB = playerB(mid);
if(sA > sB) minm = min(minm, mid), e = mid-1;
else s = mid+1;
mid = (s+e)/2;
}
if(minm != LONG_LONG_MAX) {
cout<<minm<<endl;
return 0;
}
}
cout<<"-1"<<endl;
return 0;
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGZ1bmN0aW9uYWw+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGNmbG9hdD4KI2luY2x1ZGUgPGNsaW1pdHM+CiNpbmNsdWRlIDxjc3RkbGliPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8bW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