#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)(1e5) + 2;
const int mod = (int)(1e9) + 7;
const double eps = 1e-15;
int comp(double a, double b) {
  if (fabs(a - b) < eps) return 0;
  if (a - b > eps) return 1;
  return -1;
}
int n, a[133][7], m[7], cnt, d[11], bad[11], ans = -1, cur;
bool check(int mid) {
  int s1 = 0, s2 = 0;
  for (int i = 1; i <= 5; i++) {
    if (m[i]) {
      long long kol = d[i], total = mid + cur;
      int val = 3500;
      int z = 32;
      for (; z >= 1; z /= 2) {
        val -= 500;
        if (kol * z <= total) break;
      }
      if (a[1][i] != -1) s1 += val - (a[1][i] * val / 250);
      if (a[2][i] != -1) s2 += val - (a[2][i] * val / 250);
    } else {
      long long kol = d[i] + mid, total = cur + mid;
      if (bad[i]) kol = d[i];
      if (bad[i] && !cnt) total = cur;
      int val = 3500;
      int z = 32;
      for (; z >= 1; z /= 2) {
        val -= 500;
        if (kol * 1ll * z <= total) break;
      }
      if (a[1][i] != -1) s1 += val - (a[1][i] * val / 250);
      if (a[2][i] != -1) s2 += val - (a[2][i] * val / 250);
    }
  }
  return s1 > s2;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= 5; j++) cin >> a[i][j];
  for (int i = 1; i <= n; i++) {
    bool ok = false;
    for (int j = 1; j <= 5; j++)
      if (a[i][j] != -1) ok = true, d[j]++;
    if (ok) cur++;
  }
  for (int i = 1; i <= 5; i++) {
    if ((a[1][i] != -1 && a[2][i] != -1 && a[1][i] < a[2][i]) ||
        (a[1][i] != -1 && a[2][i] == -1))
      m[i] = 1;
    if (a[1][i] == -1)
      bad[i] = 1;
    else
      cnt++;
  }
  for (int i = 0; i <= 1e5; i++) {
    if (check(i)) {
      cout << i;
      return 0;
    }
  }
  cout << ans;
  return 0;
}
