#include <bits/stdc++.h>
using namespace std;
const int INF = int(1e9);
const long long INF64 = INF * 1ll * INF;
const long double PI = acosl(-1.0);
const int MOD = INF + 7;
const long double EPS = 1e-9;
int fracs[] = {32, 16, 8, 4, 2, 1};
int score[] = {3000, 2500, 2000, 1500, 1000, 500};
int deg[5];
int borderl[6];
int borderr[6];
int minv[5];
int minp[5];
int cntacc[5];
int n;
bool check_num(int first) {
  first += n;
  borderl[0] = 0;
  borderr[0] = first / 32;
  borderl[1] = borderr[0] + 1;
  borderr[1] = first / 16;
  borderl[2] = borderr[1] + 1;
  borderr[2] = first / 8;
  borderl[3] = borderr[2] + 1;
  borderr[3] = first / 4;
  borderl[4] = borderr[3] + 1;
  borderr[4] = first / 2;
  borderl[5] = borderr[4] + 1;
  borderr[5] = first;
  first -= n;
  for (deg[0] = 0; deg[0] < 6; deg[0]++)
    for (deg[1] = 0; deg[1] < 6; deg[1]++)
      for (deg[2] = 0; deg[2] < 6; deg[2]++)
        for (deg[3] = 0; deg[3] < 6; deg[3]++)
          for (deg[4] = 0; deg[4] < 6; deg[4]++) {
            bool flag = true;
            for (int i = 0; i < 5; i++) {
              int minacc = cntacc[i];
              int maxacc = (minv[i] == -1 ? minacc : minacc + first);
              if (max(minacc, borderl[deg[i]]) > min(maxacc, borderr[deg[i]]))
                flag = false;
            }
            if (!flag) continue;
            int vscore = 0;
            int pscore = 0;
            for (int i = 0; i < 5; i++) {
              if (minv[i] != -1)
                vscore += (score[deg[i]]) - (score[deg[i]] / 250 * minv[i]);
              if (minp[i] != -1)
                pscore += (score[deg[i]]) - (score[deg[i]] / 250 * minp[i]);
            }
            if (vscore > pscore) return true;
          }
  return false;
}
int main() {
  cin >> n;
  for (int i = 0; i < int(n); i++) {
    for (int j = 0; j < int(5); j++) {
      int k;
      cin >> k;
      if (k != -1) cntacc[j]++;
      if (i == 0) minv[j] = k;
      if (i == 1) minp[j] = k;
    }
  }
  for (int i = 0; i < 141; i++)
    if (check_num(i)) {
      cout << i << endl;
      return 0;
    }
  if (!check_num(MOD)) {
    puts("-1");
    return 0;
  }
  int l = 140;
  int r = MOD + 1;
  while (r > l + 1) {
    int mid = (l + r) >> 1;
    if (check_num(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << endl;
  return 0;
}
