#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
template <class T>
inline void chkmin(T &a, T b) {
  if (a > b) a = b;
}
template <class T>
inline void chkmax(T &a, T b) {
  if (a < b) a = b;
}
int bounds[7];
int quantity[5];
int qq[2][5];
int scoreDiff[5];
int n;
int CalcScore(int j, int t) {
  if (t == -1) return 0;
  int c = 1;
  if (quantity[j] <= n / 2) ++c;
  if (quantity[j] <= n / 4) ++c;
  if (quantity[j] <= n / 8) ++c;
  if (quantity[j] <= n / 16) ++c;
  if (quantity[j] <= n / 32) ++c;
  return c * 2 * (250 - t);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    if (i < 2) {
      for (int j = 0; j < 5; ++j) {
        cin >> qq[i][j];
        if (qq[i][j] != -1) ++quantity[j];
      }
      continue;
    }
    for (int j = 0; j < 5; ++j) {
      int c;
      cin >> c;
      if (c != -1) ++quantity[j];
    }
  }
  for (int i = 0; i < 5; ++i) {
    scoreDiff[i] = CalcScore(i, qq[1][i]) - CalcScore(i, qq[0][i]);
  }
  for (int q = 0; q < 10000; ++q) {
    int res = 0;
    for (int i = 0; i < 5; ++i)
      res += CalcScore(i, qq[0][i]) - CalcScore(i, qq[1][i]);
    if (res > 0) {
      cout << q << '\n';
      return 0;
    }
    ++n;
    for (int i = 0; i < 5; ++i)
      if (scoreDiff[i] > 0 && qq[0][i] != -1) ++quantity[i];
  }
  cout << -1 << '\n';
}
