#include <bits/stdc++.h>
using namespace std;
int cnt[5], a[5][120];
int sc(int x, int y) {
  if ((x << 5) <= y)
    return 3000;
  else if ((x << 4) <= y)
    return 2500;
  else if ((x << 3) <= y)
    return 2000;
  else if ((x << 2) <= y)
    return 1500;
  else if ((x << 1) <= y)
    return 1000;
  return 500;
}
int dyn_sc(int s, int t) { return s - s / 250 * t; }
bool can(int m, int n) {
  int v = 0, p = 0, y;
  for (int i = 0; i < 5; ++i) {
    if (a[i][0] < a[i][1])
      y = sc(cnt[i], n + m);
    else if (a[i][0] != 250)
      y = sc(cnt[i] + m, n + m);
    else
      y = sc(cnt[i], n + m);
    v += dyn_sc(y, a[i][0]);
    p += dyn_sc(y, a[i][1]);
  }
  return v > p;
}
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int j = 0; j < n; ++j) {
    for (int i = 0; i < 5; ++i) {
      cin >> a[i][j];
      if (a[i][j] != -1)
        ++cnt[i];
      else
        a[i][j] = 250;
    }
  }
  int res = -1;
  for (int i = 0; i <= 31 * n; ++i) {
    if (can(i, n)) {
      res = i;
      break;
    }
  }
  cout << res << '\n';
  return 0;
}
