#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1<<30
#define LINF 1LL<<60
int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	ll N; cin >> N;
	//vector<pair<pair<ll, ll>, ll>> seg(N);
	ll Seg[360] = { 0 };
	for (int i = 0; i < N;i++) {
		ll M, D, S, V; cin >> M >> D >> V >> S; D--;
		for (int j = 0; j < V;j++) {
			Seg[((M - 1) * 30 + D + j) % 360] = max(Seg[((M - 1) * 30 + D + j) % 360], S);
		}
		for (int j = 1; j <= S; j++) {
			Seg[((M - 1) * 30 + D - j + 360 * 2) % 360] = max(Seg[((M - 1) * 30 + D - j + 360 * 2) % 360], S - j);
			Seg[((M - 1) * 30 + D + V + j - 1) % 360] = max(Seg[((M - 1) * 30 + D + V + j - 1) % 360], S - j);
		}
	}
	ll ans = INF;
	for (int i = 0;i < 360; i++) {
		ans = min(ans, Seg[i]);
	}
	cout << ans << endl;
	return 0;
}