#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int Start[100];
int s[100];
int End[100];
int v[100];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N;
	cin >> N;
	for(int i = 0; i < N; i++) {
		int M, D;
		cin >> M >> D >> v[i] >> s[i];
		Start[i] = 30 * (M - 1) + D;
		End[i] = Start[i] + v[i] - 1;
	}

	int ans = 1e9;
	for(int i = 0; i < 360; i++) {
		int x = 0;
		for(int j = 0; j < N; j++) {
			if(Start[j] <= i && i <= End[j]) {
				x = max(x, s[j]);
			}
			else if(Start[j] <= i + 360 && i + 360 <= End[j]) {
				x = max(x, s[j]);
			}
			else {
				int t = 1e9;
				t = min({ t, abs(Start[j] - i), abs(End[j] - i) });
				t = min({ t, abs(Start[j] + 360 - i), abs(End[j] + 360 - i) });
				t = min({ t, abs(Start[j] - 360 - i), abs(End[j] - 360 - i) });
				x = max({ x, s[j] - t });
				/*if(i < Start[j]) {
					int t = s[j] - min(Start[j] - i, i + 360 - End[j]);
					x = max(x, t);
					}
					else {
					int t = s[j] - min(i - End[j], Start[j] + 360 - i);
					x = max(x, t);
					}*/
			}
		}

		ans = min(ans, x);
	}
	cout << ans << endl;
}