#include <bits/stdc++.h>
using namespace std;
long long score_frac(long long minutes) {
  if (minutes == -1) {
    return 0;
  } else {
    return 250 - minutes;
  }
}
long long v_score, p_score;
vector<pair<long long, pair<long long, long long> > > options(
    vector<vector<long long> > &timings, long long n,
    vector<long long> &num_solved) {
  v_score = p_score = 0;
  vector<pair<long long, pair<long long, long long> > > ops;
  for (long long i = 0; i < 5; ++i) {
    long long v = timings[0][i];
    long long p = timings[1][i];
    long long v_frac = score_frac(v);
    long long p_frac = score_frac(p);
    long long rel = v_frac - p_frac;
    long long num = num_solved[i];
    long long worth;
    if (n < 2 * num) {
      worth = 500;
    } else if (n < 4 * num) {
      worth = 1000;
    } else if (n < 8 * num) {
      worth = 1500;
    } else if (n < 16 * num) {
      worth = 2000;
    } else if (n < 32 * num) {
      worth = 2500;
    } else {
      worth = 3000;
    }
    v_score += v_frac * worth;
    p_score += p_frac * worth;
    if (rel == 0) {
      continue;
    }
    if (rel > 0 || v == -1) {
      long long v1 = num - (n - num);
      if (v1 > 0) {
        ops.push_back(make_pair(v1, make_pair(i, (1000 - worth) * rel)));
      }
      long long v2 = 3 * num - (n - num);
      if (v2 > 0) {
        ops.push_back(make_pair(v2, make_pair(i, (1500 - worth) * rel)));
      }
      long long v3 = 7 * num - (n - num);
      if (v3 > 0) {
        ops.push_back(make_pair(v3, make_pair(i, (2000 - worth) * rel)));
      }
      long long v4 = 15 * num - (n - num);
      if (v4 > 0) {
        ops.push_back(make_pair(v4, make_pair(i, (2500 - worth) * rel)));
      }
      long long v5 = 31 * num - (n - num);
      if (v5 > 0) {
        ops.push_back(make_pair(v5, make_pair(i, (3000 - worth) * rel)));
      }
    } else {
      long long v1 = (n - num) - num;
      if (v1 > 0) {
        ops.push_back(make_pair(v1, make_pair(i, (1000 - worth) * rel)));
      }
      long long v2 = (n - num + 2) / 3 - num;
      if (v2 > 0) {
        ops.push_back(make_pair(v2, make_pair(i, (1500 - worth) * rel)));
      }
      long long v3 = (n - num + 6) / 7 - num;
      if (v3 > 0) {
        ops.push_back(make_pair(v3, make_pair(i, (2000 - worth) * rel)));
      }
      long long v4 = (n - num + 14) / 15 - num;
      if (v4 > 0) {
        ops.push_back(make_pair(v4, make_pair(i, (2500 - worth) * rel)));
      }
      long long v5 = (n - num + 30) / 31 - num;
      if (v5 > 0) {
        ops.push_back(make_pair(v5, make_pair(i, (3000 - worth) * rel)));
      }
    }
  }
  return ops;
}
long long solve(vector<pair<long long, pair<long long, long long> > > &options,
                long long target) {
  sort(options.begin(), options.end());
  int score = 0;
  int i = 0;
  vector<int> per_q(5, 0);
  int prev = 0;
  while (score < target || options[i].first == prev) {
    pair<long long, pair<long long, long long> > o = options[i];
    i++;
    score += o.second.second - per_q[o.second.first];
    per_q[o.second.first] = o.second.second;
    prev = o.first;
    if (i == options.size()) {
      if (score < target) {
        return -1;
      } else {
        break;
      }
    }
  }
  return options[i - 1].first;
}
int main() {
  long long n;
  cin >> n;
  vector<vector<long long> > timings;
  vector<long long> num_solved(5, 0);
  for (long long i = 0; i < n; ++i) {
    vector<long long> ts;
    for (long long j = 0; j < 5; ++j) {
      long long a;
      cin >> a;
      ts.push_back(a);
      if (a != -1) {
        num_solved[j]++;
      }
    }
    timings.push_back(ts);
  }
  vector<pair<long long, pair<long long, long long> > > ops =
      options(timings, n, num_solved);
  if (ops.size() == 0) {
    cout << -1 << endl;
    return 0;
  }
  if (v_score > p_score) {
    cout << 0 << endl;
  } else {
    long long ans = solve(ops, p_score - v_score + 1);
    cout << ans << endl;
  }
  return 0;
}
