#include <bits/stdc++.h>
using namespace std;
int parent(int i) { return i / 2; }
int leftChild(int i) { return 2 * i; }
int rightChild(int i) { return 2 * i + 1; }
void swap(int arr[][2], int a, int b) {
  int temp = arr[a][0];
  arr[a][0] = arr[b][0];
  arr[b][0] = temp;
  temp = arr[a][1];
  arr[a][1] = arr[b][1];
  arr[b][1] = temp;
}
void shiftUp(int i, int arr[][2]) {
  while (i > 1 && arr[i][0] < arr[parent(i)][0]) {
    swap(arr, i, parent(i));
    i = parent(i);
  }
}
void shiftDown(int i, int arr[][2], int size) {
  int maxIndex = i;
  int l = leftChild(i);
  if (l <= size && arr[maxIndex][0] > arr[l][0]) maxIndex = l;
  int r = rightChild(i);
  if (r <= size && arr[maxIndex][0] > arr[r][0]) maxIndex = r;
  if (maxIndex != i) {
    swap(arr, maxIndex, i);
    shiftDown(maxIndex, arr, size);
  }
}
void insert(int key1, int key2, int &size, int arr[][2]) {
  size++;
  arr[size][0] = key1;
  arr[size][1] = key2;
  shiftUp(size, arr);
}
void extractMin(int arr[][2], int &size, int *minPart, int *index) {
  *minPart = arr[1][0];
  *index = arr[1][1];
  arr[1][0] = arr[size][0];
  arr[1][1] = arr[size][1];
  size--;
  shiftDown(1, arr, size);
}
int getMin(int arr[]) { return arr[1]; }
int isEmpty(int size) {
  if (size == 0) return 1;
  return 0;
}
int getLevel(float count, float max) {
  float ratio = count / max;
  if (ratio > 1 / 2.0)
    return 1;
  else if (ratio > 1 / 4.0 && ratio <= 1 / 2.0)
    return 2;
  else if (ratio > 1 / 8.0 && ratio <= 1 / 4.0)
    return 3;
  else if (ratio > 1 / 16.0 && ratio <= 1 / 8.0)
    return 4;
  else if (ratio > 1 / 32.0 && ratio <= 1 / 16.0)
    return 5;
  else
    return 6;
}
int score(int time[][5], int count[], int max, int i) {
  float score = 0;
  for (int j = 0; j < 5; ++j) {
    if (time[i][j] != -1)
      score = score +
              (getLevel(count[j], max) * 500 * (1.0 - (time[i][j] / 250.0)));
  }
  return ceil(score);
}
int status(int time[][5], int i) {
  if (time[0][i] == time[1][i])
    return 0;
  else if (time[0][i] == -1)
    return 1;
  else if (time[1][i] == -1)
    return 2;
  else if (time[0][i] > time[1][i])
    return 1;
  else if (time[0][i] < time[1][i])
    return 2;
}
int getRatioInc(int level) { return pow(2, level - 1); }
int getRatioDec(int level) {
  if (level == 6) return 0;
  return pow(2, level);
}
int participantCal(int level, int inc, int i, int n) {
  if (inc == 2) {
    int tr = getRatioInc(level);
    return ceil(i * tr - n);
  } else {
    int tr = getRatioDec(level);
    return floor(float((n - i * tr) / (tr - 1.0)) + 1);
  }
}
long int diffScore(int count[], int n, int inc[], int time[][5],
                   long int newIndex) {
  long int score1 = 0, score2 = 0;
  for (int i = 0; i < 5; ++i) {
    int lev = 0;
    if (inc[i] == 2)
      lev = getLevel(count[i], n + newIndex);
    else if (inc[i] == 1) {
      if (time[0][i] != -1)
        lev = getLevel(count[i] + newIndex, n + newIndex);
      else
        lev = getLevel(count[i], n + newIndex);
    }
    if (time[0][i] != -1)
      score1 = score1 + 500 * lev * (1.0 - (time[0][i] / 250.0));
    if (time[1][i] != -1)
      score2 = score2 + 500 * lev * (1.0 - (time[1][i] / 250.0));
  }
  return score2 - score1;
}
int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  int time[n][5], count[5] = {0};
  for (int i = 0; i < n; ++i) {
    cin >> time[i][0] >> time[i][1] >> time[i][2] >> time[i][3] >> time[i][4];
    if (time[i][0] != -1) count[0]++;
    if (time[i][1] != -1) count[1]++;
    if (time[i][2] != -1) count[2]++;
    if (time[i][3] != -1) count[3]++;
    if (time[i][4] != -1) count[4]++;
  }
  int diff = score(time, count, n, 1) - score(time, count, n, 0);
  if (diff < 0)
    cout << "0\n";
  else {
    int len[5], inc[5], mat[7][5], k = 0, counter[5] = {0};
    int level[5] = {0};
    for (int i = 0; i < 5; ++i) {
      inc[i] = status(time, i);
      level[i] = getLevel(count[i], n);
      if (inc[i] == 2) {
        len[i] = 6 - level[i];
        k = 0;
        for (int j = level[i] + 1; j <= 6; ++j)
          mat[k++][i] = participantCal(j, 2, count[i], n);
      } else if (inc[i] == 1) {
        len[i] = level[i] - 1;
        k = 0;
        for (int j = level[i] - 1; j >= 1; --j) {
          if (time[0][i] != -1)
            mat[k++][i] = participantCal(j, 1, count[i], n);
          else
            mat[k++][i] = 32767;
        }
      } else
        len[i] = 0;
    }
    int arrHeap[10][2], size = 0;
    for (int i = 0; i < 5; ++i) {
      if (len[i] != 0) {
        insert(mat[0][i], i, size, arrHeap);
        counter[i]++;
      }
    }
    long int min = 0;
    int index, minPart;
    while (!isEmpty(size) && diff > -1 && min < 1000000007) {
      extractMin(arrHeap, size, &minPart, &index);
      int newLevel = level[index] + counter[index] + 1;
      if (counter[index] <= len[index])
        insert(mat[counter[index]++][index], index, size, arrHeap);
      if (min < minPart) min = minPart;
      diff = diffScore(count, n, inc, time, min);
    }
    if (diff < 0)
      cout << min << "\n";
    else
      cout << -1 << "\n";
  }
  return 0;
}
