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<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
using namespace std;
int inf = 1000000000;


int main(void) {
  int n;
  cin >> n;
  vector<int> m(n);
  vector<int> d(n);
  vector<int> v(n);
  vector<int> s(n);
  vector<int> c(12 * 30, 0);
  for(int i=0; i<n; i++){
    cin >> m[i] >> d[i] >> v[i] >> s[i];
    m[i]--; d[i]--;
    int tmp = m[i] * 30 + d[i];
    for(int j=tmp; j<tmp+v[i]; j++){
      if( j < 0 ) c[360+j] = max(c[360+j], s[i]);
      else if( j >= 360 ) c[j-360] = max(c[j-360], s[i]);
      else c[j] = max(c[j], s[i]);
    }
    for(int j=1; j<=s[i]; j++){
      if( tmp - j < 0 ) c[360+tmp-j] = max({0, c[360+tmp-j], s[i]-j});
      else if( tmp - j >= 360 ) c[tmp-j-360] = max({0, c[tmp-j-360], s[i]-j});
      else c[tmp-j] = max({0, c[tmp-j], s[i]-j});
    }
    for(int j=0; j<s[i]; j++){
      if( tmp+v[i]+j < 0 ) c[360+tmp+v[i]+j] = max({0, c[360+tmp+v[i]+j], s[i]-j-1});
      else if( tmp+v[i]+j >= 360 ) c[tmp+v[i]+j-360] = max({0, c[tmp+v[i]+j-360], s[i]-j-1});
      else c[tmp+v[i]+j] = max({0, c[tmp+v[i]+j], s[i]-j-1});
    }
  }

  cout << *min_element(c.begin(), c.end()) << endl;


  return 0;
}

// EOF

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;

int main() {
    int n;
    while (cin >> n) {
        vector<int> start(n), end(n), power(n);
        rep(i, n) {
            int m, d, v, s;
            cin >> m >> d >> v >> s;
            --m;
            --d;
            start[i] = m*30 + d;
            end[i] = start[i] + v - 1;
            power[i] = s;
        }

        const int Y = 30*12;
        int ans = 1e9;
        rep(d, Y) {
            int maxi = 0;
            rep(i, n) {
                int cand;
                int s = start[i], e = end[i];
                int s2 = s + Y, e2 = e - Y;
                if (s <= d && d < e) {
                    cand = power[i];
                } else if (e <= d && d < s2) {
                    cand = max(0, power[i] - min(d - e, s2 - d));
                } else {
                    cand = max(0, power[i] - min(d - e2, s - d));
                }
                maxi = max(maxi, cand);
            }
            ans = min(ans, maxi);
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repp(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)

typedef long long LL;
typedef long double LD;

int main(){
  int n;
  cin >> n;
  int m,d,v,s;
  vector<int> y(360,0);
  rep(i,n){
    cin >> m >> d >> v >> s;
    d+=30*m;
    rep(j,s){
      y[(d+v+j)%360]=max(y[(d+v+j)%360],s-j-1);
    }
    rep(j,v){
      y[(d+j)%360]=max(y[(d+j)%360],s);
    }
    rep(j,s){
      y[(d-j-1+360)%360]=max(y[(d-j-1+360)%360],s-j-1);
    }
  }
  int ans=IINF;
  rep(i,360){
    ans=min(ans,y[i]);
  }
  cout << ans << endl;
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main(void) {
  int n;
  cin >> n;

  vector<int> m(n), d(n), v(n), s(n);
  for (int i = 0; i < n; i++) {
    cin >> m[i] >> d[i] >> v[i] >> s[i];
  }
  
  
  int t[360] = {};
  for (int i = 0; i < 360; i++) {
    for (int j = 0; j < n; j++) {
      int day = (m[j] - 1) * 30 + (d[j] - 1);
      if ((i - day + 360) % 360 <= v[j] - 1) {
        t[i] = max(t[i], s[j]);
      } else {
        t[i] = max(t[i], max(0, s[j] - min((day - i + 360) % 360, (i - (day + v[j] - 1) + 360) % 360)));
      }
    }
  }

  int ans = 1e9;
  for (int i = 0; i < 360; i++) {
    ans = min(ans, t[i]);
  }

  cout << ans << endl;

  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    int data[100010] = {};
    rep(i, 0, n){
        int m, d, s, v;
        cin >> m >> d >> v >> s;
        m--; d--;
        int st = m * 30 + d;
        int ed = st + v - 1;
        rep(k, 0, 3){
            rep(j, 0, 36 * 30){
                if(st <= j && j <= ed) chmax(data[j], s);
                else chmax(data[j], max(0LL, s - min(abs(st - j), abs(j - ed))));
            }
            st += 12 * 30; ed += 12 * 30;
        }
    }
    int ans = INF;
    rep(i, 12 * 30, 24 * 30) chmin(ans, data[i]);
    cout << ans << endl;
}