#include <bits/stdc++.h>
template <class T>
T abs(T x) {
  return (x > 0) ? x : (-x);
}
template <class T>
T ceil(T a, T b) {
  return a / b + (a % b != 0);
}
int n, t[125][5], cnt[5], l, r, mid;
int a[6] = {1, 2, 4, 8, 16, 32};
std::set<int> s;
int score(int x, int y) {
  int s = y / x, i;
  for (i = 0; i < 6 && s >= a[i]; ++i)
    ;
  return 500 * i;
}
bool cal(int m) {
  int x = 0, y = 0;
  for (int i = 0, j; i < 5; ++i) {
    if (t[0][i] >= 0 && t[1][i] < 0)
      x += score(cnt[i], n + m) / 250 * (250 - t[0][i]);
    else if (t[0][i] >= 0 && t[1][i] >= 0) {
      if (t[0][i] < t[1][i])
        j = score(cnt[i], n + m);
      else
        j = score(cnt[i] + m, n + m);
      x += j / 250 * (250 - t[0][i]), y += j / 250 * (250 - t[1][i]);
    } else if (t[0][i] < 0 && t[1][i] >= 0)
      y += score(cnt[i], n + m) / 250 * (250 - t[1][i]);
  }
  return x > y;
}
int main() {
  scanf("%d\n", &n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 5; ++j) {
      scanf("%d", &t[i][j]);
      if (t[i][j] >= 0) ++cnt[j];
    }
  }
  s.insert(0);
  s.insert(1000000007 + 1);
  for (int i = 0; i < 5; ++i)
    if (t[0][i] < 0 && t[1][i] >= 0)
      for (int j = 0; j < 6; ++j) s.insert(std::max(0, cnt[i] * a[j] - n));
  for (std::set<int>::iterator it = s.begin(); it != s.end();) {
    for (l = *it, r = *(++it); l < r;) {
      mid = (l + r) >> 1;
      if (cal(mid))
        r = mid;
      else
        l = mid + 1;
    }
    if (l < *it) break;
  }
  if (l <= 1000000007)
    printf("%d\n", l);
  else
    printf("-1\n");
  return 0;
}
