#include <bits/stdc++.h>
int aa[120][5], kk[5];
int solve(int *bb, int n) {
  int k, sum1, sum2, j, bad;
  sum1 = sum2 = 0;
  for (j = 0; j < 5; j++) {
    if (aa[0][j] != -1) sum1 += (bb[j] + 1) * (250 - aa[0][j]) * 2;
    if (aa[1][j] != -1) sum2 += (bb[j] + 1) * (250 - aa[1][j]) * 2;
  }
  if (sum1 <= sum2) return n * 32 + 1;
  for (k = 0; k <= n * 32; k++) {
    bad = 0;
    for (j = 0; j < 5; j++)
      if (aa[0][j] != -1) {
        int lower = bb[j] == 5 ? 0 : (n + k) / (1 << (bb[j] + 1)) + 1 - kk[j];
        int upper = (n + k) / (1 << bb[j]) - kk[j];
        if (upper < 0 || lower > upper || lower > k) {
          bad = 1;
          break;
        }
      } else if ((bb[j] != 5 && kk[j] << (bb[j] + 1) <= (n + k)) ||
                 (kk[j] << bb[j] > (n + k))) {
        bad = 1;
        break;
      }
    if (!bad) return k;
  }
  return n * 32 + 1;
}
int main() {
  static int bb[5];
  int n, i, j, b, b_, min, k;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    for (j = 0; j < 5; j++) {
      scanf("%d", &aa[i][j]);
      if (aa[i][j] != -1) kk[j]++;
    }
  min = n * 32 + 1;
  for (b = 0; b < 7776; b++) {
    for (j = 0, b_ = b; j < 5; j++, b_ /= 6) bb[j] = b_ % 6;
    k = solve(bb, n);
    if (min > k) min = k;
  }
  printf("%d\n", min == n * 32 + 1 ? -1 : min);
  return 0;
}
