#include <iostream>
#include <algorithm> 
#include <vector>
#include <cstring>
using namespace std;
int main(void){
	int N;
	cin>>N;
	int dat[360];
	memset(dat, -1, sizeof(dat));
	for(int i=0;i<N;i++){
		int M,D,S,V;
		cin>>M>>D>>V>>S;
		for(int j=0;j<V;j++){
			int currentday = ((M-1)*30+D-1+j)%360;
			dat[currentday]=max(S,dat[currentday]);
		}
		for(int j=0;j<360;j++){
			int currentday = ((M-1)*30+D-1+j)%360;
			dat[currentday]=max(dat[currentday],max(0,S-min(360-j,(j+1-V+360)%360)));
		}
	}
	int mi=1000;
	for(int i=0;i<360;i++){
		mi=min(mi,dat[i]);
	}
	cout<<mi<<endl;
	return 0;
}