#include <bits/stdc++.h>
using namespace std;
int n, pas[10], t[200][10];
int score(int x, int m, int t) {
  int al;
  if (32 * x <= m)
    al = 3000;
  else if (16 * x <= m)
    al = 2500;
  else if (x * 8 <= m)
    al = 2000;
  else if (x * 4 <= m)
    al = 1500;
  else if (x * 2 <= m)
    al = 1000;
  else
    al = 500;
  return al - al / 250 * t;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 5; j++) {
      scanf("%d", &t[i][j]);
      if (~t[i][j]) pas[j]++;
    }
  }
  for (int x = 0; x <= 31 * n; x++) {
    int dif = 0;
    for (int i = 1; i <= 5; i++) {
      int p = pas[i];
      if (t[1][i] == -1 && t[2][i] != -1) dif -= score(p, n + x, t[2][i]);
      if (t[1][i] != -1 && t[2][i] == -1) dif += score(p, n + x, t[1][i]);
      if (t[1][i] != -1 && t[2][i] != -1) {
        if (t[1][i] > t[2][i])
          dif += score(p + x, n + x, t[1][i]) - score(p + x, n + x, t[2][i]);
        else
          dif += score(p, n + x, t[1][i]) - score(p, n + x, t[2][i]);
      }
    }
    if (dif > 0) return printf("%d\n", x), 0;
  }
  puts("-1");
}
