#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int N, x[361] = {};
  cin >> N;
  for (int i = 1; i <= N; i++) {
    int M, D, V, S;
    cin >> M >> D >> V >> S;
    int a = (M - 1) * 30 + D + V - 1;
    int b = (M - 1) * 30 + D;
    for (int k = 0; k < V; k++) x[(b + k) % 360] = max(S, x[(b + k) % 360]);
    for (int j = 1; j <= 360; j++) {
      x[(a + j) % 360] = max(S - j, x[(a + j) % 360]);
      x[(b - j + 360) % 360] = max(S - j, x[(b - j + 360) % 360]);
    }
  }
  sort(x, x + 361);
  cout << x[1] << endl;
}

