#include <bits/stdc++.h>
using namespace std;
const int costs[6] = {500, 1000, 1500, 2000, 2500, 3000};
const int treb[6] = {1, 2, 4, 8, 16, 32};
bool isCost(int t, int lvl, int cntP, int cntSubm[]) {
  return (long long)cntSubm[t] * treb[lvl] <= 1 * cntP;
}
bool nextLvl(int lvls[5]) {
  for (int i = 4; i >= 0; --i) {
    if (lvls[i] != 5) {
      ++lvls[i];
      for (int j = i + 1; j < 5; ++j) {
        lvls[j] = 0;
      }
      return true;
    }
  }
  return false;
}
const double EPS = 0.00000000001;
bool isLeader(int lvls[5], int t1[5], int t2[5]) {
  long double ans1 = 0, ans2 = 0;
  for (int t = 0; t < 5; ++t) {
    if (t1[t] != -1) {
      ans1 += (long double)costs[lvls[t]] * (1 - (long double)t1[t] / 250);
    }
    if (t2[t] != -1) {
      ans2 += (long double)costs[lvls[t]] * (1 - (long double)t2[t] / 250);
    }
  }
  return ans1 > ans2 + EPS;
}
vector<int> listLvls[50000];
int cntLvls = 0;
bool check(int cntDop, int n, int cntSubm[5], int t1[5], int t2[5]) {
  for (int t = 0; t < 5; ++t) {
    if (t1[t] != -1 && t2[t] != -1) {
      if (t1[t] >= t2[t]) {
        cntSubm[t] += cntDop;
      }
    }
  }
  int lvls[5];
  for (int t = 0; t < 5; ++t) {
    for (int lvl = 5; lvl >= 0; --lvl) {
      if (isCost(t, lvl, cntDop + n, cntSubm)) {
        lvls[t] = lvl;
        break;
      }
    }
  }
  bool res = isLeader(lvls, t1, t2);
  for (int t = 0; t < 5; ++t) {
    if (t1[t] != -1 && t2[t] != -1) {
      if (t1[t] >= t2[t]) {
        cntSubm[t] -= cntDop;
      }
    }
  }
  return res;
}
int main() {
  int n;
  cin >> n;
  int cntSubm[5];
  for (int i = 0; i < 5; ++i) {
    cntSubm[i] = 0;
  }
  int t1[5], t2[5];
  for (int j = 0; j < 5; ++j) {
    scanf("%d", &t1[j]);
    if (t1[j] != -1) {
      ++cntSubm[j];
    }
  }
  for (int j = 0; j < 5; ++j) {
    scanf("%d", &t2[j]);
    if (t2[j] != -1) {
      ++cntSubm[j];
    }
  }
  bool cor = false;
  for (int i = 0; i < 5; ++i) {
    if (t2[i] == -1 && t1[i] != -1) {
      cor = true;
      break;
    }
    if (t1[i] != -1 && t2[i] != -1 && t1[i] <= t2[i]) {
      cor = true;
      break;
    }
  }
  for (int i = 0; i < n - 2; ++i) {
    int cur;
    for (int j = 0; j < 5; ++j) {
      scanf("%d", &cur);
      if (cur != -1) {
        ++cntSubm[j];
      }
    }
  }
  vector<int> lvls(5);
  do {
    if (isLeader(lvls.data(), t1, t2)) {
      listLvls[cntLvls] = lvls;
      ++cntLvls;
    }
  } while (nextLvl(lvls.data()));
  if (cntLvls == 0) {
    cout << -1 << endl;
    return 0;
  }
  int bestAns = 1000000009;
  for (int curl = 0; curl < cntLvls; ++curl) {
    int *lvl = listLvls[curl].data();
    long long l1 = 0, l2 = 1000000007;
    for (int t = 0; t < 5; ++t) {
      if (t1[t] == -1) {
        long long mx;
        if (lvl[t] != 5)
          mx = ((long long)cntSubm[t]) * treb[lvl[t] + 1] - n - 1;
        long long mn = (long long)cntSubm[t] * treb[lvl[t]] - n;
        l1 = max(l1, mn);
        l2 = min(l2, mx);
        continue;
      }
      long long mn = l1;
      if (lvl[t] != 5) {
        int tmp = int((1.0 * cntSubm[t] * treb[lvl[t] + 1] - n) /
                      (1 - treb[lvl[t] + 1]));
        for (mn = tmp - 1; mn <= tmp + 2; ++mn) {
          if (lvl[t] == 5 ||
              ((long long)cntSubm[t] + mn) * treb[lvl[t] + 1] > (mn + n)) {
            break;
          }
        }
      }
      mn = max(mn, (long long)cntSubm[t] * treb[lvl[t]] - n);
      l1 = max(l1, mn);
    }
    if (l1 <= l2) {
      bestAns = min(bestAns, int(l1));
    }
  }
  if (bestAns <= 1000000007) {
    cout << bestAns << endl;
  } else {
    cout << -1 << endl;
  }
}
