#include <bits/stdc++.h>
using namespace std;
int Score(int s, int t, int x) {
  int p;
  for (int i = 0; i <= 5 && s * (1 << i) <= t; ++i, p = 500 * i)
    ;
  return p * (1 - double(x) / 250) * (x != -1);
}
int n, t, a, s[5], v[5], p[5];
int main() {
  int n;
  cin >> n;
  t = n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 5; j++) {
      cin >> a;
      !i ? v[j] = a : i == 1 ? p[j] = a : 0;
      a != -1 ? ++s[j] : 0;
    }
  }
  for (int i = 0; i < 32 * n; i++) {
    for (int j = 0; j < 5; j++)
      if (p[j] == -1 && v[j] != -1)
        t = n + i;
      else if (p[j] != -1) {
        t = n + i;
        if (i && v[j] >= p[j]) s[j]++;
      }
    a = 0;
    for (int j = 0; j < 5; j++)
      a += Score(s[j], t, v[j]) - Score(s[j], t, p[j]);
    if (a > 0) return cout << i << endl, 0;
  }
  cout << -1 << endl;
}
