#include <bits/stdc++.h>
int n;
int mat[3][6];
int solv[6];
int pt[6];
int main() {
  memset(solv, 0, sizeof solv);
  memset(mat, -1, sizeof mat);
  scanf("%d", &n);
  int i, j;
  for (i = 0; i < 2; i++)
    for (j = 0; j < 5; j++) {
      scanf("%d", &mat[i][j]);
      if (mat[i][j] != -1) solv[j]++;
    }
  for (i = 2; i < n; i++)
    for (j = 0; j < 5; j++) {
      int a;
      scanf("%d", &a);
      if (a != -1) solv[j]++;
    }
  for (i = 0; i <= 31 * n; i++) {
    for (j = 0; j < 5; j++) {
      int cur = solv[j];
      if (mat[0][j] != -1 && mat[1][j] != -1 && mat[1][j] < mat[0][j]) cur += i;
      pt[j] = 500;
      while (pt[j] < 3000 && cur * 2 <= n + i) pt[j] += 500, cur *= 2;
    }
    int sc1 = 0, sc2 = 0;
    for (j = 0; j < 5; j++) {
      if (mat[0][j] != -1) sc1 += pt[j] / 250 * (250 - mat[0][j]);
      if (mat[1][j] != -1) sc2 += pt[j] / 250 * (250 - mat[1][j]);
    }
    if (sc1 > sc2) {
      printf("%d\n", i);
      return 0;
    }
  }
  printf("-1\n");
  return 0;
}
