#include <bits/stdc++.h>
using namespace std;
int n;
int solved[5];
int t[5], r[5];
int max_score(long long sol, long long total) {
  int score = 500;
  for (long long i = 2; i <= 32; i <<= 1) {
    if (sol * i > total) return score;
    score += 500;
  }
  return 3000;
}
int test(int k) {
  int my = 0, other = 0;
  for (int i = 0; i < 5; i++) {
    int mx;
    if (t[i] < r[i] or t[i] == 250)
      mx = max_score(solved[i], n + k);
    else
      mx = max_score(solved[i] + k, n + k);
    int mys = mx - t[i] * (mx / 250);
    int os = mx - r[i] * (mx / 250);
    my += mys;
    other += os;
  }
  return my > other;
}
int search() {
  int lo = 0, hi = 1000000008;
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    if (test(mid))
      hi = mid;
    else
      lo = mid + 1;
  }
  return lo;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int v[5];
    for (int j = 0; j < 5; j++) {
      cin >> v[j];
      if (v[j] == -1)
        v[j] = 250;
      else
        solved[j]++;
    }
    if (i == 0)
      memcpy(t, v, sizeof(v));
    else if (i == 1)
      memcpy(r, v, sizeof(v));
  }
  for (int i = 0; i <= 10000; i++) {
    if (test(i)) {
      cout << i << '\n';
      return 0;
    }
  }
  cout << "-1\n";
  return 0;
}
