#include <iostream>
#include <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
//#include <unordered_map> //???????????\??¢??°
#include <fstream> //ifstream, ofstream


using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???
#ifdef TEST
#define dout cout
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout dummy.str(""); dummy.clear(stringstream::goodbit); dummy //dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§???????????????????????????????????? http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUT2TEXTFILE????????????????????¬???????????????????????????????????¢????????????
//#define OUTPUT2TEXTFILE //*******************************************************************************************************************************************
#ifdef OUTPUT2TEXTFILE
#define dout outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
//TEST??????????????????????????¨???outputfile??????????????????????????????
#define OUTPUTFILENAME "output.txt"
ofstream outputfile(OUTPUTFILENAME);
#define OutputFilePath "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/output.txt"
#endif



#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A) dout << (A) << " "
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)

#define dispAll(A,n) dout << #A << " = "; rep(j, 0, (n)) {disP(A[j]);} dout << endl


const int INF = 2e9-1;

int MD2Day(int M, int D) {
    return (M-1)*30 + (D-1);
}

int day2M(int day) {
    return day/30 + 1;
}

int day2D(int day) {
    return day%30 + 1;
}

int main(){

    int N;
    cin >> N;
    
    int M, D, V, S;
    int d[360][2]; //day[i][0] = 0:normal day, 1:holiday / d[][1] = S
    int h[N][3]; //holiday[i][0] = start day, h[][1] = V (duration), h[][2] = S
    
    rep(i,0,360) {
        rep(j,0,2) {
            d[i][j] = 0;
        }
    }
    
    //initialization according to input
    int day;
    rep(i,0,N) {
        cin >> M >> D >> V >> S;
        
        day = MD2Day(M, D);
//        disP(M); disP(D); disp(day);
        
        h[i][0] = day;
        h[i][1] = V;
        h[i][2] = S;
        
        rep(j,0,V) {
            d[(day+j)%360][0] = 1;
            d[(day+j)%360][1] = max(d[(day+j)%360][1], S);
        }
    }
    
    int start;
    int duration;
    int influence;
    
    //calc. influence of weekday
//    dout << "calc. influence of weekday" << endl;
    
    rep(i,0,N) {
//        dout << "a "; disp(i);
        
        start = h[i][0];
        duration = h[i][1];
        influence = h[i][2];
        
        start += duration;
        
//        dout << "b "; disp(i);
        //forward direction
        rep(j,0,360) {
            influence--;
            
//            if((start+j)%360==38) {
//                dout << "-----"; disp(i);
//                dout<<"forward " << j << endl;
//                disp(start);
//                disp((start+j));
//                disp((start+j)%360);
//                disp(d[(start+j)%360][1]);
//                disp(influence);
//                disp(max( d[(start+j)%360][1], influence ));
//            }
            
            d[(start+j)%360][1] = max( d[(start+j)%360][1], influence );
        }
        
//        dout << "c "; disp(i);
        start = h[i][0] - 1;
        influence = h[i][2];
        
//        dout << "d "; disp(i);
        //backward direction
        rep(j,0,360) {
//            disp((start-j+360)%360);
            
            influence--;
            
//            if((start-j+360)%360==38) {
//                dout << "-----"; disp(i);
//                dout<<"backward " << j << endl;
//                disp(start);
//                disp((start-j+360));
//                disp((start-j+360)%360);
//                disp(d[(start-j+360)%360][1]);
//                disp(influence);
//                disp(max( d[(start-j+360)%360][1], influence ));
//            }
            
            d[(start-j+360)%360][1] = max( d[(start-j+360)%360][1], influence );
        }
        
        
//        dout << "e "; disp(i);
    }
    
    
    int minInfluence = 361;
    int minDay = -1;
    
    //find the least day
    rep(i,0,360) {
        
        if( minInfluence > d[i][1] ) {
            minInfluence = d[i][1];
            minDay = i;
        }
    }
    
//    dout << "=====\n";
    cout << minInfluence << endl;

//    disp(minDay);
    
    
//    rep(i,0,360) {
//        disP(day2M(i)); dout <<"/"; disP(day2D(i)); dout<<"i="; disP(i); disP(d[i][0]); disp(d[i][1]);
//    }
    

#ifdef OUTPUT2TEXTFILE
outputfile.close();
cout << "\"" << OutputFilePath << "\"" << endl;
#endif

return 0;
}

Measured memory_bytes: 3088