#include <bits/stdc++.h>
using namespace std;
const long long bots = 1000000000 + 7;
int cnt[5], a[5], b[5];
int n;
int get_cost(long long n, long long k) {
  if (32LL * k <= n) return 3000;
  if (16LL * k <= n) return 2500;
  if (8LL * k <= n) return 2000;
  if (4LL * k <= n) return 1500;
  if (2LL * k <= n) return 1000;
  return 500;
}
bool win(long long m, long long add) {
  vector<long long> c(5);
  for (int i = 0; i < (int)(5); ++i) c[i] = cnt[i];
  for (int i = 0; i < (int)(5); ++i) {
    if (a[i] != 250 && a[i] > b[i]) c[i] += m;
  }
  int A = 0, B = 0;
  for (int i = 0; i < (int)(5); ++i) {
    int cost = get_cost(n + m + add, c[i]);
    A += cost * (250 - a[i]);
    B += cost * (250 - b[i]);
  }
  if (A > B) return true;
  return false;
}
void solve() {
  cin >> n;
  for (int i = 0; i < (int)(5); ++i) {
    cin >> a[i];
    if (a[i] != -1) ++cnt[i];
    if (a[i] == -1) a[i] = 250;
  }
  for (int j = 0; j < (int)(5); ++j) {
    cin >> b[j];
    if (b[j] != -1) ++cnt[j];
    if (b[j] == -1) b[j] = 250;
  }
  for (int i = 0; i < (int)(n - 2); ++i) {
    for (int j = 0; j < (int)(5); ++j) {
      int x;
      cin >> x;
      if (x != -1) {
        cnt[j] += 1;
      }
    }
  }
  long long ans = 1LL << 58;
  int values[6] = {3000, 2500, 2000, 1500, 1000, 500};
  for (int m = 0; m < (int)(100000); ++m) {
    vector<long long> c(5);
    for (int i = 0; i < (int)(5); ++i) c[i] = cnt[i];
    for (int i = 0; i < (int)(5); ++i) {
      if (a[i] != 250 && a[i] > b[i]) c[i] += m;
    }
    for (int id = 0; id < (int)(5); ++id) {
      for (int valpos = 0; valpos < (int)(6); ++valpos) {
        int val = values[valpos];
        long long lo = 0;
        long long hi = 100 * bots;
        while (lo < hi) {
          long long mi = (lo + hi) / 2;
          if (get_cost(n + m + mi, c[id]) <= val)
            hi = mi;
          else
            lo = mi + 1;
        }
        if (win(m, lo)) ans = min(ans, m + lo);
      }
    }
    for (int id = 0; id < (int)(5); ++id) {
      for (int valpos = 0; valpos < (int)(6); ++valpos) {
        if (valpos == 0) continue;
        int val = values[valpos];
        long long lo = 0;
        long long hi = 100 * bots;
        while (lo < hi) {
          long long mi = (lo + hi) / 2;
          if (get_cost(n + m + mi, c[id]) < val)
            hi = mi;
          else
            lo = mi + 1;
        }
        if (win(m, lo)) ans = min(ans, m + lo);
      }
    }
  }
  if (ans > bots) ans = -1;
  cout << ans << endl;
}
void testgen() {
  FILE* f = fopen("input.txt", "w");
  fclose(f);
}
int main(int argc, char* argv[]) {
  solve();
  return 0;
}
