#include <bits/stdc++.h>
using namespace std;
double k1[10];
double k2[10];
double t[10];
double f[10];
int fun(double x, double y, double t) {
  double f1 = 1.0 / 2;
  double f2 = 1.0 / 4;
  double f3 = 1.0 / 8;
  double f4 = 1.0 / 16;
  double f5 = 1.0 / 32;
  if (t == -1) return 0;
  if (y / x > f1) {
    return 500 * (1 - t / 250);
  } else if (y / x > f2) {
    return 1000 * (1 - t / 250);
  } else if (y / x > f3) {
    return 1500 * (1 - t / 250);
  } else if (y / x > f4) {
    return 2000 * (1 - t / 250);
  } else if (y / x > f5) {
    return 2500 * (1 - t / 250);
  } else {
    return 3000 * (1 - t / 250);
  }
}
int main() {
  int i, j, k, l1, f1, f2, f3, f4, f5, t4, t1, m;
  int T;
  int r1, l2, r2;
  int t3, n;
  cin >> n;
  memset(k1, 0, sizeof(k1));
  memset(k2, 0, sizeof(k2));
  memset(t, 0, sizeof(t));
  memset(f, 0, sizeof(f));
  for (i = 1; i <= n; i++) {
    cin >> f1 >> f2 >> f3 >> f4 >> f5;
    if (i == 1) {
      k1[1] = f1;
      k1[2] = f2;
      k1[3] = f3;
      k1[4] = f4;
      k1[5] = f5;
    } else if (i == 2) {
      k2[1] = f1;
      k2[2] = f2;
      k2[3] = f3;
      k2[4] = f4;
      k2[5] = f5;
    }
    if (f1 != -1) {
      t[1]++;
    }
    if (f2 != -1) {
      t[2]++;
    }
    if (f3 != -1) {
      t[3]++;
    }
    if (f4 != -1) {
      t[4]++;
    }
    if (f5 != -1) {
      t[5]++;
    }
  }
  int sum1 = 0, sum2 = 0;
  for (i = 1; i <= 5; i++) {
    if ((k1[i] < k2[i] && k1[i] != -1) || k2[i] == -1) {
      sum1 += fun(32, 1, k1[i]);
      sum2 += fun(32, 1, k2[i]);
    } else if (k1[i] != -1) {
      sum1 += fun(1, 1, k1[i]);
      sum2 += fun(1, 1, k2[i]);
    } else {
      sum2 += fun(n, t[i], k2[i]);
    }
  }
  if (sum1 <= sum2) {
    cout << "-1" << endl;
    return 0;
  }
  sum1 = 0;
  sum2 = 0;
  for (i = 1; i <= 5; i++) {
    sum1 += fun(n, t[i], k1[i]);
    sum2 += fun(n, t[i], k2[i]);
  }
  if (sum1 > sum2) {
    cout << "0" << endl;
    return 0;
  }
  m = 0;
  int gg2 = 0;
  while (1) {
    for (i = 1; i <= 5; i++) {
      if ((k1[i] < k2[i] && k1[i] != -1) || k2[i] == -1) {
        t[i] = t[i];
      } else if (k1[i] != -1) {
        t[i]++;
      } else {
        t[i] = t[i];
      }
    }
    m++;
    sum1 = 0;
    sum2 = 0;
    for (i = 1; i <= 5; i++) {
      sum1 += fun(n + m, t[i], k1[i]);
      sum2 += fun(n + m, t[i], k2[i]);
    }
    if (sum1 > sum2) break;
    gg2++;
    if (gg2 > 32 * 122) break;
  }
  if (sum1 > sum2)
    cout << m << endl;
  else
    cout << "-1" << endl;
  return 0;
}
