#include <bits/stdc++.h>
using namespace std;
int s1[5], s2[5], kk[5];
int m, n;
bool solve(int t1, int t2, int j) {
  if (j == 5) return t1 > t2;
  int k = kk[j];
  for (int e = 0; e <= 5; e++) {
    int p = (e + 1) * 500;
    int d = 1 << e;
    if (s1[j] == 0) {
      if (k * d <= m && (e == 5 || m < k * d * 2))
        if (solve(t1 + p * s1[j], t2 + p * s2[j], j + 1)) return true;
    } else {
      int lower = e == 5 ? 0 : max(0, m - k * d * 2 + 1);
      int upper = min((m - n) * d * 2, m * 2 - k * d * 2);
      if (lower <= upper)
        if (solve(t1 + p * s1[j], t2 + p * s2[j], j + 1)) return true;
    }
  }
  return false;
}
int main() {
  cin >> n;
  cin >> s1[0] >> s1[1] >> s1[2] >> s1[3] >> s1[4];
  cin >> s2[0] >> s2[1] >> s2[2] >> s2[3] >> s2[4];
  for (int j = 0; j < 5; j++) {
    if (s1[j] != -1) {
      kk[j]++;
      s1[j] = 250 - s1[j];
    } else
      s1[j] = 0;
    if (s2[j] != -1) {
      kk[j]++;
      s2[j] = 250 - s2[j];
    } else
      s2[j] = 0;
  }
  for (int i = 2; i < n; i++)
    for (int j = 0; j < 5; j++) {
      int a;
      cin >> a;
      if (a != -1) kk[j]++;
    }
  int k = 0;
  for (int j = 0; j < 5; j++) k = max(k, kk[j]);
  int n_ = max(k * 32, n + n + 1);
  for (m = n; m <= n_; m++)
    if (solve(0, 0, 0)) {
      cout << m - n << endl;
      return 0;
    }
  cout << -1 << endl;
  return 0;
}
