#include <bits/stdc++.h>
using namespace std;
int n, a[155][6], dem[155], cost[155], tong[155];
void nhap() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= 5; j++) {
      cin >> a[i][j];
      if (a[i][j] != -1) dem[j]++;
    }
}
void init() {}
void solve() {
  for (int them = 0; them <= 31 * n + 2; them++) {
    for (int j = 1; j < 6; j++) {
      int total = n + them, cur = dem[j];
      if (a[1][j] != -1 && a[2][j] != -1 && a[1][j] > a[2][j]) cur += them;
      cost[j] = 500;
      while (cost[j] < 3000 && 2 * cur <= total) cur *= 2, cost[j] += 500;
    }
    for (int i = 1; i < 3; i++) {
      tong[i] = 0;
      for (int j = 1; j < 6; j++)
        if (a[i][j] != -1) tong[i] += cost[j] / 250 * (250 - a[i][j]);
    }
    if (tong[1] > tong[2]) {
      printf("%d\n", them);
      return;
    }
  }
  {
    cout << "-1\n";
    return;
  }
}
int main() {
  nhap();
  solve();
  return 0;
}
