#include <bits/stdc++.h>
using namespace std;
int64_t m_score(int64_t con, int64_t sol) {
  int64_t ret = 0;
  for (int64_t i = 0, p = 1; i < 6; i++, p <<= 1) {
    if (sol * p <= con) ret += 500;
  }
  return ret;
}
int32_t main() {
  int64_t n;
  cin >> n;
  int64_t cnt[5];
  int64_t sol[n][5];
  for (auto &i : sol)
    for (int64_t &j : i) cin >> j;
  for (int64_t i = 0; i < 5; i++) {
    cnt[i] = 0;
    for (int64_t j = 0; j < n; j++)
      if (sol[j][i] != -1) cnt[i]++;
  }
  vector<int64_t> mp(1, 0);
  for (int64_t i = 0; i < 5; i++) {
    if (1) {
      int64_t mcnt = m_score(n, cnt[i]);
      for (int64_t sc = mcnt - 500; sc > 0; sc -= 500) {
        int64_t L = 0, R = 1e9 + 8;
        while (R - L > 1) {
          int64_t M = (R + L) / 2;
          if (m_score(n + M, cnt[i]) <= sc)
            R = M;
          else
            L = M;
        }
        mp.push_back(R);
      }
    }
    if (1) {
      int64_t mcnt = m_score(n, cnt[i]);
      for (int64_t sc = mcnt + 500; sc <= 5000; sc += 500) {
        int64_t L = 0, R = 1e9 + 8;
        while (R - L > 1) {
          int64_t M = (R + L) / 2;
          if (m_score(n + M, cnt[i]) >= sc)
            R = M;
          else
            L = M;
        }
        mp.push_back(R);
      }
    }
  }
  if (!mp.empty()) sort(mp.begin(), mp.end());
  for (int64_t L : mp) {
    if (L > 1e9 + 7) break;
    int64_t as = 0, bs = 0;
    for (int64_t i = 0; i < 5; i++) {
      int64_t sc = cnt[i] + L;
      if (sol[0][i] < sol[1][i] || sol[1][i] == -1) sc -= L;
      int64_t x = m_score(n + L, sc);
      int64_t oas = as, obs = bs;
      if (sol[0][i] != -1) as += x - (x / 250) * sol[0][i];
      if (sol[1][i] != -1) bs += x - (x / 250) * sol[1][i];
    }
    if (as > bs) {
      cout << L;
      return 0;
    }
  }
  cout << -1;
  return 0;
}
