Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	seq := make([]int, 360)
	N := io.NextInt()
	for i := 0; i < N; i++ {
		M, D, V, S := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
		M = (M - 1) * 30
		D--
		for j := M + D; j < M+D+V; j++ {
			seq[j%360] = intMax(S, seq[j%360])
		}
		for j := 0; j < S; j++ {
			seq[(M+D-j+360)%360] = intMax(S-j, seq[(M+D-j+360)%360])
			seq[(M+D+V+j-1)%360] = intMax(S-j, seq[(M+D+V+j-1)%360])
		}
	}
	ans := 360
	for _, val := range seq {
		ans = intMin(val, ans)
	}
	io.Println(ans)
}

func intMin(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func intMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #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;
}