#include <bits/stdc++.h>
inline int read() {
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9')) c = getchar();
  int k = 0, kk = 1;
  if (c == '-') c = getchar(), kk = -1;
  while (c >= '0' && c <= '9') k = k * 10 + c - '0', c = getchar();
  return kk * k;
}
using namespace std;
void write(int x) {
  if (x < 0) x = -x, putchar('-');
  if (x) write(x / 10), putchar(x % 10 + '0');
}
void writeln(int x) {
  write(x);
  if (x == 0) putchar('0');
  puts("");
}
int n, a[1000000][6], f[10], ans, dd, bei, kk, kkk;
struct lsg {
  int x, y;
} b[1000000];
bool pd(lsg x, lsg y) { return x.x < y.x || x.x == y.x && x.y < y.y; }
signed main() {
  n = read();
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= 5; j++) a[i][j] = read(), f[j] += a[i][j] >= 0;
  for (int i = 1; i <= 5; i++) {
    if (a[1][i] == -1) a[1][i] = 250;
    if (a[2][i] == -1) a[2][i] = 250;
    kk = 0;
    bei = 1;
    while (f[i] <= n && kk < 12) kk += 2, f[i] *= 2, bei *= 2;
    ans += (a[2][i] - a[1][i]) * kk;
    if (a[1][i] <= a[2][i] || a[1][i] == 250) {
      f[i] /= bei;
      dd = f[i];
      while (bei <= 32) {
        b[++kkk].x = f[i] * bei - n;
        b[kkk].y = (a[2][i] - a[1][i]) * 2;
        bei *= 2;
      }
    } else {
      f[i] /= bei;
      dd = n - f[i];
      bei /= 2;
      while (bei > 1) {
        b[++kkk].x = (dd / (bei - 1) + 1) - f[i];
        b[kkk].y = (a[1][i] - a[2][i]) * 2;
        bei /= 2;
      }
    }
  }
  sort(b + 1, b + 1 + kkk, pd);
  for (int i = 1; i <= kkk; i++) {
    if (ans > 0) {
      cout << b[i - 1].x << endl;
      return 0;
    }
    ans += b[i].y;
  }
  if (ans > 0)
    cout << b[kkk].x << endl;
  else
    puts("-1");
}
