#include <bits/stdc++.h>
using namespace std;
template <class T, class S>
ostream& operator<<(ostream& os, const pair<T, S>& v) {
  return os << "(" << v.first << ", " << v.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "[";
  for (int i = int(0); i <= int((static_cast<int>((v).size())) - 1); ++i) {
    if (i) os << ", ";
    os << v[i];
  }
  return os << "]";
}
template <class T>
bool setmax(T& _a, T _b) {
  if (_a < _b) {
    _a = _b;
    return true;
  }
  return false;
}
template <class T>
bool setmin(T& _a, T _b) {
  if (_b < _a) {
    _a = _b;
    return true;
  }
  return false;
}
template <class T>
T gcd(T _a, T _b) {
  return _b == 0 ? _a : gcd(_b, _a % _b);
}
vector<int> read_vi(int n = -1) {
  if (n < 0) scanf("%d", &n);
  vector<int> a(n);
  for (int i = int(0); i <= int((n)-1); ++i) scanf("%d", &a[i]);
  return a;
}
vector<long long> read_vl(int n = -1) {
  if (n < 0) scanf("%d", &n);
  vector<long long> a(n);
  for (int i = int(0); i <= int((n)-1); ++i) scanf("%lld", &a[i]);
  return a;
}
vector<double> read_vd(int n = -1) {
  if (n < 0) scanf("%d", &n);
  vector<double> a(n);
  for (int i = int(0); i <= int((n)-1); ++i) scanf("%lf", &a[i]);
  return a;
}
vector<pair<int, int> > read_vpi(int n = -1) {
  if (n < 0) scanf("%d", &n);
  vector<pair<int, int> > a(n);
  for (int i = int(0); i <= int((n)-1); ++i)
    scanf("%d%d", &a[i].first, &a[i].second);
  return a;
}
vector<pair<long long, long long> > read_vpl(int n = -1) {
  if (n < 0) scanf("%d", &n);
  vector<pair<long long, long long> > a(n);
  for (int i = int(0); i <= int((n)-1); ++i)
    scanf("%lld%lld", &a[i].first, &a[i].second);
  return a;
}
vector<pair<double, double> > read_vpd(int n = -1) {
  if (n < 0) scanf("%d", &n);
  vector<pair<double, double> > a(n);
  for (int i = int(0); i <= int((n)-1); ++i)
    scanf("%lf%lf", &a[i].first, &a[i].second);
  return a;
}
inline long long powmod(long long a, long long b, long long m) {
  long long r = 1;
  for (; b > 0; b >>= 1, a = a * a % m) {
    if (b & 1) r = r * a % m;
  }
  return r;
}
int base_score(int x, int y) {
  if (x * 32 <= y) return 3000;
  if (x * 16 <= y) return 2500;
  if (x * 8 <= y) return 2000;
  if (x * 4 <= y) return 1500;
  if (x * 2 <= y) return 1000;
  return 500;
}
int score(int x, int y, int t) {
  if (t < 0) return 0;
  int s = base_score(x, y);
  return s - s / 250 * t;
}
int main() {
  int num;
  scanf("%d", &num);
  vector<vector<int> > a(num, vector<int>(5));
  vector<int> solved(5);
  for (int i = int(0); i <= int((num)-1); ++i)
    for (int j = int(0); j <= int((5) - 1); ++j) {
      scanf("%d", &a[i][j]);
      if (a[i][j] >= 0) solved[j]++;
    }
  for (int ans = int(0); ans <= int((100000) - 1); ++ans) {
    vector<int> additional(5);
    for (int j = int(0); j <= int((5) - 1); ++j)
      if (a[0][j] >= 0) {
        if (a[1][j] >= 0 && a[1][j] < a[0][j]) {
          additional[j] += ans;
        }
      }
    vector<int> s(2);
    for (int i = int(0); i <= int((2) - 1); ++i)
      for (int j = int(0); j <= int((5) - 1); ++j) {
        s[i] += score(solved[j] + additional[j], num + ans, a[i][j]);
      }
    if (s[0] > s[1]) {
      printf("%d\n", ans);
      return 0;
    }
  }
  printf("-1\n");
  return 0;
}
