#include <bits/stdc++.h>
using namespace std;
inline int nxt() {
  int x;
  scanf("%d", &x);
  return x;
}
template <class T>
bool uin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
const int N = 120 + 5;
const int M = 1e9 + 7;
int n, ac[N], tim[2][5];
bool is(int mask, int x) {
  int vs = 0, pe = 0, tot = n + x, pt;
  double cr;
  for (int i = 0; i < (int)(5); ++i) {
    double fr;
    cr = ac[i];
    if ((mask & (1 << i))) {
      if (tim[0][i] == -1) {
        return false;
      } else
        cr += x;
    }
    fr = (double)cr / tot;
    if ((cr * 32) / tot <= 1)
      pt = 3000;
    else if ((cr * 16) / tot <= 1)
      pt = 2500;
    else if ((cr * 8) / tot <= 1)
      pt = 2000;
    else if ((cr * 4) / tot <= 1)
      pt = 1500;
    else if ((cr * 2) / tot <= 1)
      pt = 1000;
    else
      pt = 500;
    if (tim[0][i] != -1) vs += pt * (250 - tim[0][i]) / 250;
    if (tim[1][i] != -1) pe += pt * (250 - tim[1][i]) / 250;
  }
  return vs > pe;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < (int)(n); ++i)
    for (int j = 0; j < (int)(5); ++j) {
      int x;
      cin >> x;
      if (i < 2) tim[i][j] = x;
      if (x != -1) ac[j]++;
    }
  int x = 0;
  for (; x <= 10000; x++) {
    for (int mask = 0; mask < (int)(32); ++mask) {
      if (is(mask, x)) {
        cout << x;
        return 0;
      }
    }
  }
  cout << -1;
}
