#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> cnt[7];
pair<pair<long long, long long>, pair<long long, long long> > frac[10];
long long n, arr[7], arr2[7];
long long comp(long long a, long long b, long long c, long long d) {
  long long res = a * d - b * c;
  if (res > 0) return 1;
  if (res == 0) return 0;
  if (res < 0) return -1;
}
long long mx(long long fo2, long long t7t) {
  long long st = 0;
  for (int i = 1; i <= 6; i++) {
    st += 500;
    if (comp(fo2, t7t, frac[i].first.first, frac[i].first.second) == 1 &&
        comp(fo2, t7t, frac[i].second.first, frac[i].second.second) <= 0)
      return st;
  }
  return st;
}
long long check(long long ac) {
  long long vas = 0, pat = 0;
  for (int i = 1; i <= 5; i++) {
    if (arr[i] == -1) {
      if (arr2[i] != -1) {
        long long fo2 = cnt[i].first, t7t = cnt[i].second + ac,
                  val = mx(fo2, t7t);
        pat += (val - (val / 250) * arr2[i]);
      }
    } else {
      if (arr2[i] == -1) {
        long long fo2 = cnt[i].first, t7t = cnt[i].second + ac,
                  val = mx(fo2, t7t);
        vas += (val - (val / 250) * arr[i]);
      } else if (arr2[i] >= arr[i]) {
        long long fo2 = cnt[i].first, t7t = cnt[i].second + ac,
                  val = mx(fo2, t7t);
        vas += (val - (val / 250) * arr[i]);
        pat += (val - (val / 250) * arr2[i]);
      } else if (arr2[i] < arr[i]) {
        long long fo2 = cnt[i].first + ac, t7t = cnt[i].second + ac,
                  val = mx(fo2, t7t);
        vas += (val - (val / 250) * arr[i]);
        pat += (val - (val / 250) * arr2[i]);
      }
    }
  }
  return pat - vas;
}
int main() {
  cin >> n;
  frac[1].first = {1, 2}, frac[1].second = {1, 1};
  frac[2].first = {1, 4}, frac[2].second = {1, 2};
  frac[3].first = {1, 8}, frac[3].second = {1, 4};
  frac[4].first = {1, 16}, frac[4].second = {1, 8};
  frac[5].first = {1, 32}, frac[5].second = {1, 16};
  frac[6].first = {-1, 1}, frac[6].second = {1, 32};
  for (int i = 1; i <= 5; i++) {
    cin >> arr[i];
    if (arr[i] >= 0) cnt[i].first++;
    cnt[i].second = n;
  }
  for (int i = 1; i <= 5; i++) {
    cin >> arr2[i];
    if (arr2[i] >= 0) cnt[i].first++;
  }
  for (int i = 3; i <= n; i++) {
    for (int j = 1; j <= 5; j++) {
      long long tmp;
      cin >> tmp;
      if (tmp >= 0) cnt[j].first++;
    }
  }
  long long ans = 1e18;
  for (long long i = 0; i <= n * 1000; i++) {
    if (check(i) < 0) {
      ans = i;
      break;
    }
  }
  if (ans > 10000000000)
    cout << "-1\n";
  else
    cout << ans << "\n";
  return 0;
}
