#include <bits/stdc++.h>
using namespace std;
const int N = 111111;
const int M = 10;
int a[N][M];
int cnt[M];
int w[M], s[M];
int n, m;
int calc(int x, int y) {
  if (x * 2 > y) {
    return 500;
  }
  if (x * 4 > y) {
    return 1000;
  }
  if (x * 8 > y) {
    return 1500;
  }
  if (x * 16 > y) {
    return 2000;
  }
  if (x * 32 > y) {
    return 2500;
  }
  return 3000;
}
int score(int ac, int attempt, int time) {
  int full = calc(ac, attempt);
  return full * (250 - time) / 250;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 5; j++) {
      cin >> a[i][j];
      if (a[i][j] != -1) {
        cnt[j]++;
      }
    }
  }
  for (int p = 0; p < 3926; p++) {
    s[1] = s[2] = 0;
    for (int j = 1; j <= 5; j++) {
      int ac = cnt[j];
      int attempt = p + n;
      if (a[1][j] != -1 && a[2][j] != -1 && a[1][j] > a[2][j]) {
        ac += p;
      }
      for (int x = 1; x <= 2; x++) {
        if (a[x][j] == -1) {
          continue;
        }
        s[x] += score(ac, attempt, a[x][j]);
      }
    }
    if (s[1] > s[2]) {
      cout << p << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
