#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:108777216")
using namespace std;
int const MAX_N = 512 * 1024;
int const INT_INF = 1000000000;
double const EPS = 1e-8;
int n, partic;
int s[128][5];
int total_ac[5];
int bg[5], en[5];
int IS_OK = 0;
inline int get_cost(int ac) {
  if (ac * 2 > partic) return 500;
  if (ac * 4 > partic) return 1000;
  if (ac * 8 > partic) return 1500;
  if (ac * 16 > partic) return 2000;
  if (ac * 32 > partic) return 2500;
  return 3000;
}
void rec(int from) {
  if (IS_OK) return;
  if (from >= 5) {
    int sum[2];
    for (int i = 0; i < 2; i++) {
      sum[i] = 0;
      for (int j = 0; j < 5; j++)
        if (s[i][j] != -1) {
          int x = get_cost(total_ac[j]);
          sum[i] += x - x * s[i][j] / 250;
        }
    }
    if (sum[0] > sum[1]) IS_OK = 1;
    return;
  }
  int last_cost = -1;
  for (int a = bg[from]; a <= en[from]; a++) {
    if (a > 0) total_ac[from] += a;
    int x = get_cost(total_ac[from]);
    if (x != last_cost) {
      rec(from + 1);
      last_cost = x;
    }
    if (a > 0) total_ac[from] -= a;
    if (IS_OK) return;
  }
}
int prev_mn_cost[5], prev_mx_cost[5], mn_cost[5], mx_cost[5];
int main() {
  clock_t t_BG = clock();
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 5; j++) {
      scanf("%d", &s[i][j]);
      if (s[i][j] != -1) total_ac[j]++;
    }
  for (int j = 0; j < 5; j++) prev_mn_cost[j] = -8, prev_mx_cost[j] = -5;
  for (int mx = 0;; mx++) {
    for (int j = 0; j < 5; j++) {
      bg[j] = -mx;
      en[j] = 0;
      if (s[0][j] != -1) en[j] = mx;
    }
    partic = n + mx;
    IS_OK = 0;
    for (int j = 0; j < 5; j++) mn_cost[j] = INT_INF, mx_cost[j] = -INT_INF;
    for (int j = 0; j < 5; j++)
      for (int a = bg[j]; a <= en[j]; a++) {
        int q = total_ac[j];
        if (a > 0) q += a;
        int x = get_cost(q);
        mn_cost[j] = min(mn_cost[j], x);
        mx_cost[j] = max(mx_cost[j], x);
      }
    int is_diff = 0;
    for (int j = 0; j < 5; j++)
      if (mn_cost[j] != prev_mn_cost[j] || mx_cost[j] != prev_mx_cost[j])
        is_diff = 1;
    if (is_diff) rec(0);
    if (IS_OK) {
      printf("%d", mx);
      return 0;
    }
    if ((((double)(clock() - t_BG)) / (CLOCKS_PER_SEC)) > 1.45) {
      printf("-1");
      return 0;
    }
    for (int j = 0; j < 5; j++)
      prev_mn_cost[j] = mn_cost[j], prev_mx_cost[j] = mx_cost[j];
  }
  return 0;
}
