#include <bits/stdc++.h>
using namespace std;
long long C;
long long A[5];
int vasya[5], petya[5];
inline int score(int t, int v) { return t != -1 ? v - (v / 250) * t : 0; }
bool itWorks(const long long &k, int pos = 0, int V = 0, int P = 0) {
  if (pos == 5) {
    return V > P;
  }
  long long ant = -1LL, act, sumo = vasya[pos] == -1 ? 0 : 32LL * k;
  for (int pot = 0; pot <= 5; pot++, ant = act) {
    act = (1LL << pot) * (C + k);
    if (max(ant + 1, A[pos]) > min(act, A[pos] + sumo)) continue;
    int v = 3000 - 500 * pot;
    if (itWorks(k, pos + 1, V + score(vasya[pos], v), P + score(petya[pos], v)))
      return true;
  }
  return false;
}
int main() {
  int W;
  scanf("%d", &W);
  C = W;
  for (int i = 0; i < 5; i++) {
    scanf("%d", &vasya[i]);
    if (vasya[i] != -1) A[i]++;
  }
  for (int i = 0; i < 5; i++) {
    scanf("%d", &petya[i]);
    if (petya[i] != -1) A[i]++;
  }
  for (int i = 2; i < W; i++) {
    for (int j = 0; j < 5; j++) {
      int a;
      scanf("%d", &a);
      if (a != -1) A[j]++;
    }
  }
  for (int i = 0; i < 5; i++) A[i] *= 32;
  for (int ini = 0; ini <= 5000; ini++) {
    if (itWorks(ini)) {
      printf("%d\n", ini);
      return 0;
    }
  }
  int ini = 0, fin = 1e9 + 7;
  while (ini < fin) {
    long long piv = (ini + fin) / 2;
    if (itWorks(piv))
      fin = piv;
    else
      ini = piv + 1;
  }
  if (itWorks(ini)) {
    printf("%d\n", ini);
  } else
    printf("-1\n");
}
