#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:671088640")
using namespace std;
const int MOD = 1000000007;
const long long INF = 1000000000000000010;
const long double EPS = 1e-7;
const int HASH_POW = 29;
const long double PI = acos(-1.0);
mt19937_64 rnd(1);
double workTime() { return double(clock()) / CLOCKS_PER_SEC; }
void my_return(int code) { exit(code); }
int n, t[120][5], cnt[5], delta[5];
const int L[6] = {16, 8, 4, 2, 1, 0};
const int R[6] = {32, 16, 8, 4, 2, 1};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < 5; ++j) scanf("%d", &t[i][j]);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < 5; ++j)
      if (t[i][j] != -1) ++cnt[j];
  for (int j = 0; j < 5; ++j)
    if (t[0][j] != -1) delta[j] += 250 - t[0][j];
  for (int j = 0; j < 5; ++j)
    if (t[1][j] != -1) delta[j] -= 250 - t[1][j];
  const int MAGIC = 6 * 6 * 6 * 6 * 6;
  int ans = 1000000007;
  for (int msk = 0; msk < MAGIC; ++msk) {
    int s = msk, score = 0;
    for (int i = 0; i < 5; ++i) {
      int cur = 2 * (s % 6 + 1);
      s /= 6;
      score += delta[i] * cur;
    }
    if (score > 0) {
      int minAns = 0, maxAns = 1000000007;
      int s = msk;
      for (int i = 0; i < 5; ++i) {
        int pos = s % 6;
        s /= 6;
        if (t[0][i] == -1) {
          int ll = (32 * cnt[i] + R[pos] - 1) / R[pos] - n;
          int rr;
          if (L[pos] == 0)
            rr = 1000000007;
          else
            rr = (32 * cnt[i] - 1) / L[pos] - n;
          minAns = max(minAns, ll);
          maxAns = min(maxAns, rr);
        } else {
          int ll = -1, rr = 10000007;
          while (ll + 1 < rr) {
            int mid = (ll + rr) / 2;
            int mn = L[pos] * (n + mid) / 32 - cnt[i];
            if ((mn + cnt[i]) * 32 == L[pos] * (n + mid)) ++mn;
            int mx = (R[pos] * (n + mid)) / 32 - cnt[i];
            if (mn <= mx && max(0, mn) <= min(mid, mx))
              rr = mid;
            else
              ll = mid;
          }
          minAns = max(minAns, rr);
        }
      }
      if (minAns <= maxAns) ans = min(ans, minAns);
    }
  }
  if (ans == 1000000007)
    printf("-1\n");
  else
    printf("%d\n", ans);
  my_return(0);
}
