#include<cstdio>

inline int getNum(int d, int m){
    int ans = 0;
    int arr[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    for(int i = 1; i < m; i++)
        ans += arr[i];
    ans += d;
    return ans + 100;
}

int main(){
    FILE * inputFile = fopen("input.txt","r");
    int n;
    fscanf(inputFile, "%d\n", &n);
    int cntReq[470] = {0};
    for(int i = 0; i < n; i++){
        int m, d, p, t;
        fscanf(inputFile, "%d %d %d %d\n", &m, &d, &p, &t);
        int ref = getNum(d, m);
        for(int j = 1; j <= t; j++)
            cntReq[ref - j] += p;
    }
    int mx = 0;
    for(int i = 0; i < 470; i++)
        if(cntReq[i] > mx)
            mx = cntReq[i];
    FILE * outputFile = fopen("output.txt","w");
    fprintf(outputFile, "%ld\n", mx);
    fclose(outputFile);
    return 0;
}


#include<iostream>

using namespace std;

inline int getNum(int d, int m){
    int ans = 0;
    int arr[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    for(int i = 1; i < m; i++)
        ans += arr[i];
    ans += d;
    return ans + 100;
}

int main(){
    ios :: sync_with_stdio(false);
    int n;
    cin >> n;
    int cntReq[470] = {0};
    for(int i = 0; i < n; i++){
        int m, d, p, t;
        cin >> m >> d >> p >> t;
        int ref = getNum(d, m);
        for(int j = 1; j <= t; j++)
            cntReq[ref - j] += p;
    }
    int mx = 0;
    for(int i = 0; i < 470; i++)
        if(cntReq[i] > mx)
            mx = cntReq[i];
    cout << mx;
    return 0;
}


#include<iostream>

using namespace std;

inline int getNum(int d, int m){
    int ans = 0;
    int arr[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    for(int i = 1; i < m; i++)
        ans += arr[i];
    ans += d;
    return ans + 100;
    //Just shift the index
}

int main(){
    ios :: sync_with_stdio(false);
    int n;
    cin >> n;
    int cntReq[470] = {0};
    for(int i = 0; i < n; i++){
        int m, d, p, t;
        cin >> m >> d >> p >> t;
        int ref = getNum(d, m);
        for(int j = 1; j <= t; j++)
            cntReq[ref - j] += p;
    }
    int mx = 0;
    for(int i = 0; i < 470; i++)
        if(cntReq[i] > mx)
            mx = cntReq[i];
    cout << mx;
    return 0;
}


#include <iostream>
#include <fstream>
using namespace std;
int v[100005];
int mes[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main(){
    int n;
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    cin>>n;
    for(int i=0,m,d,p,t;i<n;i++){
        cin>>m>>d>>p>>t;
        int dias=365;
        for(int j=1;j<m;j++)dias+=mes[j];
        dias+=d-1;
        for(int j=1;j<=t;j++)v[dias-j]+=p;
    }
    int ans=0;
    for(int i=0;i<100005;i++)ans=max(ans,v[i]);
    cout<<ans;
}


#include <iostream>
#include <fstream>
using namespace std;
int v[100005];
int mes[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main(){
    int n;
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    cin>>n;
    for(int i=0,m,d,p,t;i<n;i++){
        cin>>m>>d>>p>>t;
        int dias=365;
        for(int j=1;j<=m;j++)dias+=mes[j];
        dias+=d;
        for(int j=1;j<=t;j++)v[dias-j]+=p;
    }
    int ans=0;
    for(int i=0;i<100005;i++)ans=max(ans,v[i]);
    cout<<ans;
}


#include <iostream>
using namespace std;
int v[100005];
int mes[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main(){
	int n;
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	cin>>n;
	for(int i=0,m,d,p,t;i<n;i++){
		cin>>m>>d>>p>>t;
		int dias=365;
		for(int j=1;j<=m;j++)dias+=mes[j];
		dias+=d;
		for(int j=1;j<=t;j++)v[dias-j]+=p;
	}
	int ans=0;
	for(int i=0;i<100005;i++)ans=max(ans,v[i]);
	cout<<ans;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

//#define finp "B.in"
//#define fout "B.out"
#define finp "input.txt"
#define fout "output.txt"

#define maxdy 365
#define maxt 100

const int d_of_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
    //#ifndef ONLINE_JUDGE
		freopen(finp,"r", stdin);
		freopen(fout,"w", stdout);
	//#endif

	int res[maxdy + maxt] = {};
    int sumdm[12];

	sumdm[0] = d_of_month[0];
	for (int i=1; i<12; i++)
        sumdm[i] = sumdm[i-1] + d_of_month[i];

    int n;
    cin >> n;

    for (int i=0; i<n; i++)
    {
        int m, p, d, t;
        cin >> m >> d >> p >> t;
        int idx_st = sumdm[m] + d - t + maxt;
        int idx_ed = sumdm[m] + d + maxt;
        for (int j=idx_st; j<idx_ed; j++)
            res[j] += p;
    }

    cout << *max_element(res, res+maxdy + maxt);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

//#define finp "B.in"
//#define fout "B.out"
#define finp "input.txt"
#define fout "output.txt"

#define maxdy 365
#define maxt 100

const int d_of_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
    //#ifndef ONLINE_JUDGE
		freopen(finp,"r", stdin);
		freopen(fout,"w", stdout);
	//#endif

	int res[maxdy + maxt] = {};
    int sumdm[12];

	sumdm[0] = d_of_month[0];
	for (int i=1; i<12; i++)
        sumdm[i] = sumdm[i-1] + d_of_month[i];

    int n;
    cin >> n;

    for (int i=0; i<n; i++)
    {
        int m, p, d, t;
        cin >> m >> d >> p >> t;
        int idx_st = sumdm[m-1] + d - t + maxt;
        int idx_ed = sumdm[m-1] + d + maxt;
        //cout << idx_st << '\n';
        //cout << idx_ed << '\n';

        for (int j=idx_st; j<idx_ed; j++)
            res[j] += p;
    }

    cout << *max_element(res, res+maxdy + maxt);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

#define finp "input.txt"
#define fout "output.txt"
//#define finp "B.in"
//#define fout "B.out"

#define maxdy 365
#define maxt 100

const int d_of_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
    //#ifndef ONLINE_JUDGE
		freopen(finp,"r", stdin);
		freopen(fout,"w", stdout);
	//#endif
    int res[maxdy + maxt] = {0};
    int sumdm[12];

	sumdm[0] = 0;
	for (int i=1; i<12; i++)
        sumdm[i] = sumdm[i-1] + d_of_month[i-1];

    int n;
    cin >> n;

    for (int i=0; i<n; i++)
    {
        int m, p, d, t;
        cin >> m >> d >> p >> t;
        int idx_st = sumdm[m-1] + d - t + maxt;
        int idx_ed = sumdm[m-1] + d + maxt;
        //cout << idx_st << '\n';
        //cout << idx_ed << '\n';
        for (int j=idx_st; j<idx_ed; j++)
            res[j] += p;
    }

    cout << *max_element(res, res+maxdy + maxt);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

#define finp "B.in"
#define fout "B.out"

#define maxdy 365
#define maxt 100

const int d_of_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
    #ifndef ONLINE_JUDGE
		//freopen(finp,"r", stdin);
		//freopen(fout,"w", stdout);
	#endif

	int res[maxdy + maxt] = {};
    int sumdm[12];

	sumdm[0] = d_of_month[0];
	for (int i=1; i<12; i++)
        sumdm[i] = sumdm[i-1] + d_of_month[i];

    int n;
    cin >> n;

    for (int i=0; i<n; i++)
    {
        int m, p, d, t;
        cin >> m >> d >> p >> t;
        // trans m,d,p to indexes segment, then increase it by t;
        int idx_st = sumdm[m] + d - t + maxt;
        int idx_ed = sumdm[m] + d + maxt;
        //cout << idx_st << ' ' << idx_ed << '\n';
        for (int j=idx_st; j<idx_ed; j++)
            res[j] += p;
    }

    cout << *max_element(res, res+maxdy + maxt);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

#define finp "B.in"
#define fout "B.out"

#define maxdy 365
#define maxt 100

const int d_of_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
    #ifndef ONLINE_JUDGE
		freopen(finp,"r", stdin);
		freopen(fout,"w", stdout);
	#endif

	int res[maxdy + maxt] = {};
    int sumdm[12];

	sumdm[0] = d_of_month[0];
	for (int i=1; i<12; i++)
        sumdm[i] = sumdm[i-1] + d_of_month[i];

    int n;
    cin >> n;

    for (int i=0; i<n; i++)
    {
        int m, p, d, t;
        cin >> m >> d >> p >> t;
        // trans m,d,p to indexes segment, then increase it by t;
        int idx_st = sumdm[m] + d - t + maxt;
        int idx_ed = sumdm[m] + d + maxt;
        //cout << idx_st << ' ' << idx_ed << '\n';
        for (int j=idx_st; j<idx_ed; j++)
            res[j] += p;
    }

    cout << *max_element(res, res+maxdy + maxt);
    return 0;
}


// Made by Botbaev Daryn
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <cstdio>
#include <queue>

#define ll long long
#define mp make_pair
#define pb push_back

using namespace std;

const int MAXN = 1e5+1;

long long n,m[101],d[101],p[101],t[101],pref[101],ans[1001],res;
map<ll,ll>ma;

void PrettyReckless () {
	ma[1] = 31;
	ma[2] = 28;
	ma[3] = 31;
	ma[4] = 30;
	ma[5] = 31;
	ma[6] = 30;
	ma[7] = 31;
	ma[8] = 31;
	ma[9] = 30;
	ma[10] = 31;
	ma[11] = 30;
	ma[12] = 31;
	pref[0] = 0;
	for (ll i = 1; i <= 12; ++i) {
		pref[i] = ma[i] + pref[i - 1];
	}
}

int main () {

freopen ("input.txt", "r", stdin);
freopen ("output.txt", "w", stdout);

scanf ("%I64d", &n);

for (ll i = 1; i <= n; ++i) {
	scanf ("%I64d%I64d%I64d%I64d", &m[i], &d[i], &p[i], &t[i]);
}

PrettyReckless();

for (ll i = 1; i <= n; ++i) {
	ll qwe = 100 + pref[m[i]] + d[i] - t[i];
	for (ll j = qwe; j <= qwe + t[i] - 1; ++j) {
		ans[j] += p[i];
	}
}

res =  -MAXN;

for (ll i = 1; i <= 500; ++i) {
		res = max(res, ans[i]);
}

printf ("%I64d", res);

return 0;
}


// Made by Botbaev Daryn
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <cstdio>
#include <queue>

#define ll long long
#define mp make_pair
#define pb push_back

using namespace std;

const int MAXN = 1e5+1;

int n,m[101],d[101],p[101],t[101],pref[101],ans[1001],res;
map<int,int>ma;

void PrettyReckless () {
	ma[1] = 31;
	ma[2] = 28;
	ma[3] = 31;
	ma[4] = 30;
	ma[5] = 31;
	ma[6] = 30;
	ma[7] = 31;
	ma[8] = 31;
	ma[9] = 30;
	ma[10] = 31;
	ma[11] = 30;
	ma[12] = 31;
	pref[0] = 0;
	for (int i = 1; i <= 12; ++i) {
		pref[i] = ma[i] + pref[i - 1];
	}
}

int main () {

freopen ("input.txt", "r", stdin);
freopen ("output.txt", "w", stdout);

scanf ("%d", &n);

for (int i = 1; i <= n; ++i) {
	scanf ("%d%d%d%d", &m[i], &d[i], &p[i], &t[i]);
}

PrettyReckless();

for (int i = 1; i <= n; ++i) {
	int qwe = 100 + pref[m[i]] + d[i] - t[i];
	for (int j = qwe; j <= qwe + t[i] - 1; ++j) {
		ans[j] += p[i];
	}
}

res =  -MAXN;

for (int i = 1; i <= 500; ++i) {
		res = max(res, ans[i]);
}

printf ("%d", res);

return 0;
}


// Made by Botbaev Daryn
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <cstdio>
#include <queue>

#define ll long long
#define mp make_pair
#define pb push_back

using namespace std;

const int MAXN = 1e5+1;

long long n,m[101],d[101],p[101],t[101],pref[101],ans[1001],res;
map<ll,ll>ma;

void PrettyReckless () {
	ma[1] = 31;
	ma[2] = 28;
	ma[3] = 31;
	ma[4] = 30;
	ma[5] = 31;
	ma[6] = 30;
	ma[7] = 31;
	ma[8] = 31;
	ma[9] = 30;
	ma[10] = 31;
	ma[11] = 30;
	ma[12] = 31;
	pref[0] = 0;
	for (ll i = 1; i <= 12; ++i) {
		pref[i] = ma[i] + pref[i - 1];
	}
}

int main () {

freopen ("input.txt", "r", stdin);
freopen ("output.txt", "w", stdout);

scanf ("%I64d", &n);

for (ll i = 1; i <= n; ++i) {
	scanf ("%I64d%I64d%I64d%I64d", &m[i], &d[i], &p[i], &t[i]);
}

PrettyReckless();

for (ll i = 1; i <= n; ++i) {
	ll qwe = 205 + pref[m[i]] + d[i] - t[i];
	for (ll j = qwe; j <= qwe + t[i] - 1; ++j) {
		ans[j] += p[i];
	}
}

res =  -MAXN;

for (ll i = 1; i <= 1000; ++i) {
		res = max(res, ans[i]);
}

printf ("%I64d", res);

return 0;
}


// Made by Botbaev Daryn
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <cstdio>
#include <queue>

#define ll long long
#define mp make_pair
#define pb push_back

using namespace std;

const int MAXN = 1e5+1;

long long n,m[101],d[101],p[101],t[101],pref[101],ans[1001],res;
map<ll,ll>ma;

void PrettyReckless () {
	ma[1] = 31;
	ma[2] = 28;
	ma[3] = 31;
	ma[4] = 30;
	ma[5] = 31;
	ma[6] = 30;
	ma[7] = 31;
	ma[8] = 31;
	ma[9] = 30;
	ma[10] = 31;
	ma[11] = 30;
	ma[12] = 31;
	pref[0] = 0;
	for (ll i = 1; i <= 12; ++i) {
		pref[i] = ma[i] + pref[i - 1];
	}
}

int main () {
freopen ("input.txt", "r", stdin);
freopen ("output.txt", "w", stdout);
scanf ("%I64d", &n);

for (ll i = 1; i <= n; ++i) {
	scanf ("%I64d%I64d%I64d%I64d", &m[i], &d[i], &p[i], &t[i]);
}

PrettyReckless();

for (ll i = 1; i <= n; ++i) {
	ll pp = pref[m[i] - 1] + d[i] + 110;
	for (int j = 1; j <= t[i]; ++j) {
		ans[pp - j] += p[i];
	}
}

res =  -MAXN;

for (ll i = 1; i <= 1000; ++i) {
		res = max(res, ans[i]);
}

printf ("%I64d", res);

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define mem(x) memset(x, 0, sizeof x)
#define mem1(x) memset(x, -1, sizeof x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MX 1000005
int main()
{
    freopen("input.txt","r", stdin);
    freopen("output.txt","w",stdout);
    int i,j,k, l, m, n,ans=0,mx=0,d, p, t, arr[900], days[15]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    mem(arr);
    for(i=1; i<=12; i++) days[i]+=days[i-1];
    cin>>n;
    while(n--)
    {
        cin>>m>>d>>p>>t;
        int date=days[m-1]+d+365, len=date-t;
        for(i=date-1; i>=len; i--) arr[i]+=p;
    }
    for(i=0;i<=800; i++)
        mx=max(mx,arr[i]);
    cout<<mx<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define mem(x) memset(x, 0, sizeof x)
#define mem1(x) memset(x, -1, sizeof x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MX 1000005
int arr[900], days[15]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
    int i,j,k, l, m, n,ans=0,mx=0,d, p, t;
    for(i=1; i<=12; i++) days[i]+=days[i-1];
    cin>>n;
    while(n--)
    {
        cin>>m>>d>>p>>t;
        int date=days[m-1]+d+365, len=date-t;
        for(i=date-1; i>=len; i--) arr[i]+=p;
    }
    for(i=0;i<=800; i++)
        mx=max(mx,arr[i]);
    cout<<mx<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define mem(x) memset(x, 0, sizeof x)
#define mem1(x) memset(x, -1, sizeof x)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MX 1000005
int main()
{
    int i,j,k, l, m, n,ans=0,mx=0,d, p, t, arr[900], days[15]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    mem(arr);
    for(i=1; i<=12; i++) days[i]+=days[i-1];
    cin>>n;
    while(n--)
    {
        cin>>m>>d>>p>>t;
        int date=days[m-1]+d+365, len=date-t;
        for(i=date-1; i>=len; i--) arr[i]+=p;
    }
    for(i=0;i<=800; i++)
        mx=max(mx,arr[i]);
    cout<<mx<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;

typedef struct Ol {
    int st, en;
    int cnt;
} Ol;

bool comp(const Ol& a, const Ol& b) {
    return a.en < b.en;
}

int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int N = 111;
Ol ols[N];
int sum[444];
int n;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        int m, d, p, t;
        cin >> m >> d >> p >> t;
        m--;
        d--;
        int en = 0;
        for(int i = 0; i < m; i++) {
            en += days[i];
        }
        en += d;
        en--;
        ols[i].en = en;
        ols[i].st = ols[i].en - t + 1;
        ols[i].cnt = p;
    }
    for(int i = 1; i <= n; i++) {
        sum[ols[i].st] += ols[i].cnt;
        sum[ols[i].en + 1] -= ols[i].cnt;
    }
    int ans = 0;
    for(int i = 1; i <= 400; i++) {
        sum[i] += sum[i - 1];
        ans = max(ans, sum[i]);
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;

typedef struct Ol {
    int st, en;
    int cnt;
} Ol;

bool comp(const Ol& a, const Ol& b) {
    return a.en < b.en;
}

int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int N = 111;
Ol ols[N];
int n;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        int m, d, p, t;
        cin >> m >> d >> p >> t;
        m--;
        d--;
        int en = 0;
        for(int i = 0; i < m; i++) {
            en += days[i];
        }
        en += d;
        en--;
        ols[i].en = en;
        ols[i].st = ols[i].en - t + 1;
        ols[i].cnt = p;
    }
    sort(ols + 1, ols + n + 1, comp);
    int ans = ols[1].cnt, cnt = ans, cur = ols[1].en;
    for(int i = 2; i <= n; i++) {
        if(ols[i].st <= cur) {
            cnt += ols[i].cnt;
            ans = max(ans, cnt);
        } else {
            cnt = 0;
        }
        cur = ols[i].en;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
#define endl "\n"
typedef long long ll;
using namespace std;

typedef struct Ol {
    int st, en;
    int cnt;
} Ol;

bool comp(const Ol& a, const Ol& b) {
    return a.en < b.en;
}

int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int N = 111;
Ol ols[N];
int sum[666];
int n;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    cin >> n;
    for(int i = 1; i <= n; i++) {
        int m, d, p, t;
        cin >> m >> d >> p >> t;
        m--;
        d--;
        int en = 0;
        for(int i = 0; i < m; i++) {
            en += days[i];
        }
        en += d;
        en--;
        ols[i].en = en;
        ols[i].st = ols[i].en - t + 1;
        ols[i].cnt = p;
    }
    for(int i = 1; i <= n; i++) {
        sum[ols[i].st + 100] += ols[i].cnt;
        sum[ols[i].en + 100 + 1] -= ols[i].cnt;
    }
    int ans = 0;
    for(int i = 1; i <= 500; i++) {
        sum[i] += sum[i - 1];
        ans = max(ans, sum[i]);
    }
    cout << ans << endl;
    return 0;
}


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

int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int q, m, d, p, t;
int u[400] = {0};

int calc(int month, int day)
{
	int ret = 0;
	for(int i = 1; i < month; i ++) ret += days[i];
	return ret + day;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);

	cin >> q;
	while(q --)
	{
		cin >> m >> d >> p >> t;
		int tmp = calc(m, d);
		while(t --)
		{
			u[tmp] += p;
			tmp --;
		}
	}
	int ans = 0;
	for(int i = 1; i <= 365; i ++)
		ans = max(ans, u[i]);
	cout << ans;
	return 0;
}


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

int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int q, m, d, p, t;
int u[400] = {0};

int calc(int month, int day)
{
	int ret = 0;
	for(int i = 1; i < month; i ++) ret += days[i];
	return ret + day;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);

	cin >> q;
	while(q --)
	{
		cin >> m >> d >> p >> t;
		int tmp = calc(m, d);
		cout << tmp << endl;
		for(int i = 0; i < t && tmp-i > 0; i ++)
			u[tmp-i] += p;
	}
	int ans = 0;
	for(int i = 1; i <= 365; i ++)
		ans = max(ans, u[i]);
	cout << ans;
	return 0;
}


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

int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int q, m, d, p, t;
int u[400] = {0};

int calc(int month, int day)
{
	int ret = 0;
	for(int i = 1; i < month; i ++) ret += days[i];
	return ret + day;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);

	cin >> q;
	while(q --)
	{
		cin >> m >> d >> p >> t;
		int tmp = calc(m, d);
//		cout << tmp << endl;
		for(int i = 0; i < t && tmp-i > 0; i ++)
			u[tmp-i] += p;
	}
	int ans = 0;
	for(int i = 1; i <= 365; i ++)
		ans = max(ans, u[i]);
	cout << ans;
	return 0;
}


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

int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int q, m, d, p, t;
int u[400] = {0};

int calc(int month, int day)
{
	int ret = 0;
	for(int i = 1; i < month; i ++) ret += days[i];
	return ret + day;
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> q;
	while(q --)
	{
		cin >> m >> d >> p >> t;
		int tmp = calc(m, d);
		while(t --)
		{
			u[tmp] += p;
			tmp --;
		}
	}
	int ans = 0;
	for(int i = 1; i <= 365; i ++)
		ans = max(ans, u[i]);
	cout << ans;
	return 0;
}


#include<iostream>//
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
int m, d, p, t, n,ans; vector<int>mnth[13];
void init(){
	mnth[1].resize(31); mnth[2].resize(28); mnth[3].resize(31); mnth[4].resize(30);
	mnth[5].resize(31); mnth[6].resize(30); mnth[7].resize(31); mnth[8].resize(31);
	mnth[9].resize(30); mnth[10].resize(31); mnth[11].resize(30); mnth[12].resize(31);
}
int getMax(vector<int>v){
	int mx = -1;
	for (int i = 0; i < v.size(); ++i){
		if (v[i]>mx)mx = v[i];
	}
	return mx;
}
int main()
{
	ios_base::sync_with_stdio(0);
	init();
	cin >> n;
	for (int i = 0; i < n; ++i){
		cin >> m >> d >> p >> t;

		int	mi = m,di = d-1;
			while (t>0){
				mnth[mi][di]+=p; --t;--di;
				if (di < 0){
					--mi; if (mi >= 1){
						di = mnth[mi].size()-1;
					}
					else break;
				}
			}
		}

	for (int i = 0; i <= 12;++i)
	ans = max(ans, getMax(mnth[i]));
	cout << ans << endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
int m, d, p, t, n,ans; vector<int>mnth[13];
void init(){
	mnth[1].resize(31); mnth[2].resize(28); mnth[3].resize(31); mnth[4].resize(30);
	mnth[5].resize(31); mnth[6].resize(30); mnth[7].resize(31); mnth[8].resize(31);
	mnth[9].resize(30); mnth[10].resize(31); mnth[11].resize(30); mnth[12].resize(31);
}
int getMax(vector<int>v){
	int mx = -1;
	for (int i = 0; i < v.size(); ++i){
		if (v[i]>mx)mx = v[i];
	}
	return mx;
}
int main()
{
	ios_base::sync_with_stdio(0);
	freopen("output.txt", "w",stdout);
	init();
	cin >> n;
	for (int i = 0; i < n; ++i){
		cin >> m >> d >> p >> t;

		int	mi = m,di = d-1;
			while (t>0){
				mnth[mi][di]+=p; --t;--di;
				if (di < 0){
					--mi; if (mi >= 1){
						di = mnth[mi].size()-1;
					}
					else break;
				}
			}
		}

	for (int i = 0; i <= 12;++i)
	ans = max(ans, getMax(mnth[i]));
	cout << ans << endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
int m, d, p, t, n,ans; vector<int>mnth[13];
void init(){
	mnth[1].resize(31); mnth[2].resize(28); mnth[3].resize(31); mnth[4].resize(30);
	mnth[5].resize(31); mnth[6].resize(30); mnth[7].resize(31); mnth[8].resize(31);
	mnth[9].resize(30); mnth[10].resize(31); mnth[11].resize(30); mnth[12].resize(31);
}
int getMax(vector<int>v){
	int mx = -1;
	for (int i = 0; i < v.size(); ++i){
		if (v[i]>mx)mx = v[i];
	}
	return mx;
}
int main()
{
	ios_base::sync_with_stdio(0);
	init();
	cin >> n;
	for (int i = 0; i < n; ++i){
		cin >> m >> d >> p >> t;

		int	mi = m,di = d-1;
			while (t>0){
				mnth[mi][di]+=p; --t;--di;
				if (di < 0){
					--mi; if (mi >= 1){
						di = mnth[mi].size()-1;
					}
					else break;
				}
			}
		}

	for (int i = 0; i <= 12;++i)
	ans = max(ans, getMax(mnth[i]));
	cout << ans << endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
int m, d, p, t, n,ans; vector<int>mnth[13];
void init(){
	mnth[1].resize(31); mnth[2].resize(28); mnth[3].resize(31); mnth[4].resize(30);
	mnth[5].resize(31); mnth[6].resize(30); mnth[7].resize(31); mnth[8].resize(31);
	mnth[9].resize(30); mnth[10].resize(31); mnth[11].resize(30); mnth[12].resize(31);
}
int getMax(vector<int>v){
	int mx = -1;
	for (int i = 0; i < v.size(); ++i){
		if (v[i]>mx)mx = v[i];
	}
	return mx;
}
int main()
{
	ios_base::sync_with_stdio(0);
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	init();
	cin >> n;
	for (int i = 0; i < n; ++i){
		cin >> m >> d >> p >> t;

		int	mi = m,di = d-1;
			while (t>0){
				mnth[mi][di]+=p; --t;--di;
				if (di < 0){
					--mi; if (mi >= 1){
						di = mnth[mi].size()-1;
					}
					else break;
				}
			}
		}

	for (int i = 0; i <= 12;++i)
	ans = max(ans, getMax(mnth[i]));
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int make(int d, int m) {
  int ans = d;
  for (int i = 1; i < m; i++) {
    switch(i) {
      case 1: case 3:  case 5: case 7: case 8: case 10: case 12: ans += 31; break;
      case 4: case 6: case 9: case 11: ans += 30; break;
      case 2: ans += 28; break;
    }
  }
  return ans;
}

int main() {
  int n, a[2 * 365] = {}, ans = 0;
  cin >> n;
  while (n--) {
    int m, d, p, t, i;
    cin >> m >> d >> p >> t;
    i = 365 + make(d, m) - 1;
    while (t--) {
      a[i--] += p;
    }
  }
  for (int i = 0; i < 2 * 365; i++) {
    ans = max(ans, a[i]);
  }
  cout << ans;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int make(int d, int m) {
  int ans = d;
  for (int i = 1; i < m; i++) {
    switch(i) {
      case 1: case 3:  case 5: case 7: case 8: case 10: case 12: ans += 31; break;
      case 4: case 6: case 9: case 11: ans += 30; break;
      case 2: ans += 28; break;
    }
  }
  return ans;
}

int main() {
  int n, a[2 * 365] = {}, ans = 0;
  cin >> n;
  while (n--) {
    int m, d, p, t, i;
    cin >> m >> d >> p >> t;
    i = 365 + make(d, m) - 1;
    cout << i << endl;
    while (t--) {
      a[i--] += p;
    }
  }
  for (int i = 0; i < 2 * 365; i++) {
    ans = max(ans, a[i]);
  }
  cout << ans;
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int make(int d, int m) {
  int ans = d;
  for (int i = 1; i < m; i++) {
    switch(i) {
      case 1: case 3:  case 5: case 7: case 8: case 10: case 12: ans += 31; break;
      case 4: case 6: case 9: case 11: ans += 30; break;
      case 2: ans += 28; break;
    }
  }
  return ans;
}

int main() {
  int n, a[2 * 365] = {}, ans = 0;
  ofstream fout ("output.txt");
  ifstream fin ("input.txt");
  fin >> n;
  while (n--) {
    int m, d, p, t, i;
    fin >> m >> d >> p >> t;
    i = 365 + make(d, m) - 1;
    while (t--) {
      a[i--] += p;
    }
  }
  for (int i = 0; i < 2 * 365; i++) {
    ans = max(ans, a[i]);
  }
  fout << ans;
  return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int days_of_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int months[13];
int counts[600];
int main()
{
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
    memset(counts,0,sizeof counts);
    memset(months,0,sizeof months);
    for(int i=1;i<13;i++) months[i] = days_of_month[i] + months[i-1];
    int n;
    int mon,day,t,jury;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d %d %d %d",&mon,&day,&jury,&t);
        int end = months[mon-1] + day -1;
        end += 100;
        int start = end - t + 1;
        for(int i=start;i<=end;i++) counts[i]+=jury;
    }
    cout << *max_element(counts,counts+600) << '\n';
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int days_of_month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int months[13];
int counts[600];
int main()
{
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
    memset(counts,0,sizeof counts);
    memset(months,0,sizeof months);
    for(int i=1;i<13;i++) months[i] = days_of_month[i] + months[i-1];
    int n;
    int mon,day,t,jury;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d %d %d %d",&mon,&day,&jury,&t);
        int end = months[mon] + day -1;
        end += 100;
        int start = end - t + 1;
        for(int i=start;i<=end;i++) counts[i]+=jury;
    }
    cout << *max_element(counts,counts+600) << '\n';
    return 0;
}


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

#define sc(a) scanf("%d",&a)
#define l first
#define r second

const int MAX = 111;

int mnths[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int cum [15];

int shit(int m,int d)
{
    return cum[m]+d+111;
}

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    for(int i=1;i<=12;++i)
        cum[i] = cum[i-1]+mnths[i];

    int n;
    cin>>n;

    pair<pair<int,int>,int> arr[2*MAX];

    for(int i=0;i<2*n;++i)
    {
        int m,d,t,j;
        cin>>m>>d>>j>>t;
        arr[i].l.l = shit(m,d)-1;
        arr[i].l.r = 0;
        arr[i].r = j;
        ++i;
        arr[i].l.l = shit(m,d)-t;
        arr[i].l.r = 1;
        arr[i].r = j;
    }

    sort(arr,arr+n+n);
    int res = 0;
    int cur = 0;
    for(int i=0;i<2*n;++i)
    {
        int d = arr[i].l.l;
        int state = arr[i].l.r;
        int j = arr[i].r;
        if(!state)
            cur += j;
        else
        {
            cur -= j;
            if(cur < 0)
            {
                res -= cur;
                cur = 0;
            }
        }
        //cout<<d<<" "<<state<<" "<<j<<" "<<cur<<" "<<res<<endl;
    }

    cout<<res;

	return 0;
}


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

#define sc(a) scanf("%d",&a)
#define l first
#define r second

const int MAX = 111;

int mnths[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int cum [15];

int shit(int m,int d)
{
    return cum[m]+d+111;
}

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    for(int i=1;i<=12;++i)
        cum[i] = cum[i-1]+mnths[i];

    int n;
    cin>>n;

    pair<pair<int,int>,int> arr[2*MAX];

    for(int i=0;i<2*n;++i)
    {
        int m,d,t,j;
        cin>>m>>d>>j>>t;
        arr[i].l.l = shit(m,d)-1;
        arr[i].l.r = 1;
        arr[i].r = j;
        ++i;
        arr[i].l.l = shit(m,d)-t;
        arr[i].l.r = 0;
        arr[i].r = j;
    }

    sort(arr,arr+n+n);
    int res = 0;
    int cur = 0;
    for(int i=0;i<2*n;++i)
    {
        int d = arr[i].l.l;
        int state = arr[i].l.r;
        int j = arr[i].r;
        if(state)
            cur += j;
        else
        {
            cur -= j;
            if(cur < 0)
            {
                res -= cur;
                cur = 0;
            }
        }
        //cout<<d<<" "<<state<<" "<<j<<" "<<cur<<" "<<res<<endl;
    }

    cout<<res;

	return 0;
}


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

#define sc(a) scanf("%d",&a)
#define l first
#define r second

const int MAX = 111;

int mnths[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int cum [15];

int shit(int m,int d)
{
    return cum[m]+d+111;
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    for(int i=1;i<=12;++i)
        cum[i] = cum[i-1]+mnths[i];

    int n;
    cin>>n;

    pair<pair<int,int>,int> arr[2*MAX];

    for(int i=0;i<2*n;++i)
    {
        int m,d,t,j;
        cin>>m>>d>>j>>t;
        arr[i].l.l = shit(m,d)-1;
        arr[i].l.r = 0;
        arr[i].r = j;
        ++i;
        arr[i].l.l = shit(m,d)-t;
        arr[i].l.r = 1;
        arr[i].r = j;
    }

    sort(arr,arr+n+n);
    int res = 0;
    int cur = 0;
    for(int i=0;i<2*n;++i)
    {
        int d = arr[i].l.l;
        int state = arr[i].l.r;
        int j = arr[i].r;
        if(!state)
            cur += j;
        else
        {
            cur -= j;
            if(cur < 0)
            {
                res -= cur;
                cur = 0;
            }
        }
        //cout<<d<<" "<<state<<" "<<j<<" "<<cur<<" "<<res<<endl;
    }

    cout<<res;

	return 0;
}


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

#define sc(a) scanf("%d",&a)
#define l first
#define r second

const int MAX = 111;

int mnths[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int cum [15];

int shit(int m,int d)
{
    return cum[m]+d+111;
}

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    for(int i=1;i<=12;++i)
        cum[i] = cum[i-1]+mnths[i];

    int n;
    cin>>n;

    pair<pair<int,int>,int> arr[2*MAX];

    for(int i=0;i<2*n;++i)
    {
        int m,d,t,j;
        cin>>m>>d>>j>>t;
        arr[i].l.l = shit(m,d);
        arr[i].l.r = 0;
        arr[i].r = j;
        ++i;
        arr[i].l.l = shit(m,d)-t;
        arr[i].l.r = 1;
        arr[i].r = j;
    }

    sort(arr,arr+n+n);
    int res = 0;
    int cur = 0;
    for(int i=0;i<2*n;++i)
    {
        int d = arr[i].l.l;
        int state = arr[i].l.r;
        int j = arr[i].r;
        if(!state)
            cur += j;
        else
        {
            cur -= j;
            if(cur < 0)
            {
                res -= cur;
                cur = 0;
            }
        }
        //cout<<d<<" "<<state<<" "<<j<<" "<<cur<<" "<<res<<endl;
    }

    cout<<res;

	return 0;
}


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

#define sc(a) scanf("%d",&a)
#define l first
#define r second

const int MAX = 111;

int mnths[] = {0,0,31,28,31,30,31,30,31,31,30,31,30,31};
int cum [15];

int shit(int m,int d)
{
    return cum[m]+d+111;
}

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    for(int i=1;i<=12;++i)
        cum[i] = cum[i-1]+mnths[i];

    int n;
    cin>>n;

    pair<pair<int,int>,int> arr[2*MAX];

    for(int i=0;i<2*n;++i)
    {
        int m,d,t,j;
        cin>>m>>d>>j>>t;
        arr[i].l.l = shit(m,d);
        arr[i].l.r = 0;
        arr[i].r = j;
        ++i;
        arr[i].l.l = shit(m,d)-t;
        arr[i].l.r = 1;
        arr[i].r = j;
    }

    sort(arr,arr+n+n);
    int res = 0;
    int cur = 0;
    for(int i=0;i<2*n;++i)
    {
        int d = arr[i].l.l;
        int state = arr[i].l.r;
        int j = arr[i].r;
        if(!state)
            cur += j;
        else
        {
            cur -= j;
            if(cur < 0)
            {
                res -= cur;
                cur = 0;
            }
        }
        //cout<<d<<" "<<state<<" "<<j<<" "<<cur<<" "<<res<<endl;
    }

    cout<<res;

	return 0;
}


#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
int mn[12]={31,28,31,30,31,30,31,31,30,31,30,31},v[10001],n;
int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    while(cin>>n)
    {
        int m,d,p,t,max=0;
        while(n--)
        {
            cin>>m>>d>>p>>t;
            int s=d+100;
            for(int i=1;i<m;i++)
            {
                s+=mn[i-1];
            }
            for(int i=s-t;i<s;i++)
            {
                v[i]+=p;
                if(v[i]>max)
                {
                    max=v[i];
                }
            }
        }
        cout<<max;
    }
}


#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;

int mn[12]={31,28,31,30,31,30,31,31,30,31,30,31},v[10001],n;
int main()
{
    freopen("input.txt", "r", stdin);
freopen("output..txt", "w", stdout);

    while(cin>>n)
    {
        int m,d,p,t,max=0;
        while(n--)
        {
            cin>>m>>d>>p>>t;
            int s=d+100;
            for(int i=0;i<m;i++)
            {
                s+=mn[i-1];
            }
            for(int i=s-t;i<s;i++)
            {
                v[i]+=p;
                if(v[i]>max)
                {
                    max=v[i];
                }
            }
        }
        cout<<max;
    }
}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int Days[] = { 31,28,31,30,31,30,31,31,30,30,31 };
int Juries[740] = { 0 };
int main()
{
	int n, m, d, p, t,day;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> m >> d >> p >> t;
		day = 0;
		for (int i = 0; i < m; i++)
		{
			day += Days[i];
		}
		day += d;
		day += 365;
		for (int i = day-t; i < day; i++)
		{
			Juries[i] += p;
		}
	}
	int answer = 0;
	for (int i = 0; i < 740; i++)
	{
		answer = max(answer, Juries[i]);
	}
	cout << answer << endl;

}


#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int Days[] = { 31,28,31,30,31,30,31,31,30,30,31 };
int Juries[740] = { 0 };
int main()
{
	int n, m, d, p, t,day;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> m >> d >> p >> t;
		day = 0;
		for (int i = 0; i < m; i++)
		{
			day += Days[i];
		}
		day += d;
		day + 365;
		for (int i = day-t; i < day; i++)
		{
			Juries[i] += p;
		}
	}
	int answer = 0;
	for (int i = 0; i < 740; i++)
	{
		answer = max(answer, Juries[i]);
	}
	cout << answer << endl;

}


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
int Days[] = { 31,28,31,30,31,30,31,31,30,30,31 };
int Juries[740];
int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	memset(Juries, 0, sizeof(Juries));
	int n, m, d, p, t,day;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> m >> d >> p >> t;
		day = 0;
		for (int i = 0; i < m; i++)
		{
			day += Days[i];
		}
		day += d;
		day += 365;
		for (int i = day-t; i < day; i++)
		{
			Juries[i] += p;
		}
	}
	int answer = 0;
	for (int i = 0; i < 740; i++)
	{
		answer = max(answer, Juries[i]);
	}
	cout << answer << endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
int Days[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int Juries[740];
int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	memset(Juries, 0, sizeof(Juries));
	int n, m, d, p, t,day;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> m >> d >> p >> t;
		day = 0;
		for (int i = 0; i < m-1; i++)
		{
			day += Days[i];
		}
		day += d;
		day += 366;
		for (int i = day-t; i < day; i++)
		{
			Juries[i] += p;
		}
	}
	int answer = 0;
	for (int i = 0; i < 740; i++)
	{
		answer = max(answer, Juries[i]);
	}
	cout << answer << endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
int Days[] = { 31,28,31,30,31,30,31,31,30,30,31 };
int Juries[740];
int main()
{
	memset(Juries, 0, sizeof(Juries));
	int n, m, d, p, t,day;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> m >> d >> p >> t;
		day = 0;
		for (int i = 0; i < m; i++)
		{
			day += Days[i];
		}
		day += d;
		day += 365;
		for (int i = day-t; i < day; i++)
		{
			Juries[i] += p;
		}
	}
	int answer = 0;
	for (int i = 0; i < 740; i++)
	{
		answer = max(answer, Juries[i]);
	}
	cout << answer << endl;

}


#include <bits/stdc++.h>

using namespace std;

struct meow{int a; int b; int c;};
meow mas[105];
int i,j,n,m,d,p,t,x,jabeidz[520],rez;
int dienasnosakuma[14]={110,141,169,200,230,261,291,322,353,383,414,444,475};

bool compare(const meow& aa, const meow& bb)
{
    if (aa.a==bb.a) {return aa.b<bb.b;} else {return aa.a<bb.a;}
}

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    cin>>n;
    for (i=0;i<n;++i)
    {
        cin>>m>>d>>p>>t;
        mas[i].b=dienasnosakuma[m-1]+d-1;
        mas[i].a=mas[i].b-t+1;
        mas[i].c=p;
    }
    sort(mas,mas+n,compare);
    mas[n].a=123456789;
    for (i=100;i<=515;++i)
    {
        while (mas[j].a<=i)
        {
            x+=mas[j].c;
            jabeidz[mas[j].b+1]+=mas[j].c;
            ++j;
        }
        x-=jabeidz[i];
        rez=max(rez,x);
    }

    cout<<rez;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct meow{int a; int b; int c;};

int dienasnosakuma[14]={0,141,169,200,230,261,291,322,353,383,414,444,475};

bool compare(const meow& aa, const meow& bb)
{
    if (aa.a==bb.a) {return aa.b<bb.b;} else {return aa.a<bb.a;}
}

int main()
{meow mas[105];
int i,j=0,n,m,d,p,t,x,jabeidz[520]={0},rez;
    freopen("input.txt","r",stdin);
    cin>>n;
    rez=0;
    x=0;
    for (i=0;i<n;++i)
    {
        cin>>m>>d>>p>>t;
        mas[i].b=dienasnosakuma[m]+d;
        mas[i].a=mas[i].b-t;
        mas[i].c=p;
    }
    sort(mas,mas+n,compare);
    for (i=100;i<=515;++i)
    {
        while (mas[j].a<=i) {x+=mas[j].c; jabeidz[mas[j].b+1]+=mas[j].c; ++j;}
        x-=jabeidz[i];
        rez=max(rez,x);
    }
    freopen("output.txt","w",stdout);
    cout<<rez;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct meow{int a; int b; int c;};
int dienasnosakuma[14]={0,141,169,200,230,261,291,322,353,383,414,444,475};

bool compare(const meow& aa, const meow& bb)
{
    if (aa.a==bb.a) {return aa.b<bb.b;} else {return aa.a<bb.a;}
}

int main()
{
    meow mas[105];
    int i,j=0,n,m,d,p,t,x=0,jabeidz[500]={0},rez=0;
    freopen("input.txt","r",stdin);
    cin>>n;
    for (i=0;i<n;++i)
    {
        cin>>m>>d>>p>>t;
        mas[i].b=dienasnosakuma[m]+d;
        mas[i].a=mas[i].b-t;
        mas[i].c=p;
    }
    sort(mas,mas+n,compare);
    for (i=100;i<=515;++i)
    {
        while (mas[j].a<=i) {x+=mas[j].c; jabeidz[mas[j].b+1]+=mas[j].c; ++j;}
        x-=jabeidz[i];
        rez=max(rez,x);
    }
    freopen("output.txt","w",stdout);
    cout<<rez;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <cstring>
#include <iterator>
#include <map>
#include <cstdlib>

#define mp make_pair
#define pb push_back
#define se second
#define fr first

using namespace std;

const int N = 400, inf = (int)1e9;
int n, cnt, pref[N], me[N], d[N], p[N], t[N], lay[5 * N], m[13] = {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n;
	for(int i = 1; i <= 12; i++)
		pref[i] = pref[i - 1] + m[i];
	for(int i = 1; i <= n; i++)
	{
		cin >> me[i] >> d[i] >> p[i] >> t[i];
	    lay[pref[me[i]] + d[i] + 102] += p[i];
	    lay[pref[me[i]] + d[i] + 102 - t[i] + 1] -= p[i];
	}
	int cur = 0, ans = 0;
	for(int i = 1; i <= 4 * N; i++)
	{
		if(cur += lay[i])
		{
			if(cur < 0)
			{
				ans -= cur;
				cur = 0;
			}

		}

	}
	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <cstring>
#include <iterator>
#include <map>
#include <cstdlib>

#define mp make_pair
#define pb push_back
#define se second
#define fr first

using namespace std;

const int N = 400, inf = (int)1e9;
int n, cnt[10 * N], pref[N], me[N], d[N], p[N], t[N], lay[5 * N], m[14] = {0, 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n;
	for(int i = 1; i <= 12; i++)
	pref[i] = pref[i - 1] + m[i];
	int ans = 0;
	for(int i = 1; i <= n; i++)
	{
		cin >> me[i] >> d[i] >> p[i] >> t[i];
	    for(int j = 0; j < t[i]; j++)
	    	ans = max(ans, lay[pref[me[i]] + d[i] + 102 - j] += p[i]);
	    //lay[pref[me[i]] + d[i] + 102] -= p[i];
	    //lay[pref[me[i]] + d[i] + 102 - t[i]] += p[i];
	}    /*
	int cur = 0, ans = 0;
	for(int i = 1; i < 4 * N; i++)
	{   cnt[i] = cnt[i - 1] + lay[i];
		ans = max(ans, cnt[i]);
	}   */
	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <cstring>
#include <iterator>
#include <map>
#include <cstdlib>

#define mp make_pair
#define pb push_back
#define se second
#define fr first

using namespace std;

const int N = 400, inf = (int)1e9;
int n, cnt, pref[N], me[N], d[N], p[N], t[N], lay[5 * N], m[14] = {31, 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n;
	for(int i = 1; i <= 12; i++)
		pref[i] = pref[i - 1] + m[i];
	for(int i = 1; i <= n; i++)
	{
		cin >> me[i] >> d[i] >> p[i] >> t[i];
	    lay[pref[me[i]] + d[i] + 103] += p[i];
	    lay[pref[me[i]] + d[i] + 102 - t[i] + 1] -= p[i];
	}
	int cur = 0, ans = 0;
	for(int i = 1; i < 5 * N; i++)
	{
		cur += lay[i];

			if(cur < 0)
			{
				ans -= cur;
				cur = 0;
			}

	}
	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <cstring>
#include <iterator>
#include <map>
#include <cstdlib>

#define mp make_pair
#define pb push_back
#define se second
#define fr first

using namespace std;

const int N = 400, inf = (int)1e9;
int n, cnt, pref[N], me[N], d[N], p[N], t[N], lay[5 * N], m[13] = {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n;
	for(int i = 1; i <= 12; i++)
		pref[i] = pref[i - 1] + m[i];
	for(int i = 1; i <= n; i++)
	{
		cin >> me[i] >> d[i] >> p[i] >> t[i];
	    lay[pref[me[i]] + d[i] + 103] += p[i];
	    lay[pref[me[i]] + d[i] + 102 - t[i] + 2] -= p[i];
	}
	int cur = 0, ans = 0;
	for(int i = 0; i < 4 * N; i++)
	{
		cur += lay[i];

			if(cur < 0)
			{
				ans -= cur;
				cur = 0;
			}

	}
	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <cstring>
#include <iterator>
#include <map>
#include <cstdlib>

#define mp make_pair
#define pb push_back
#define se second
#define fr first

using namespace std;

const int N = 400, inf = (int)1e9;
int n, cnt, pref[N], me[N], d[N], p[N], t[N], lay[5 * N], m[13] = {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
	cin >> n;
	for(int i = 1; i <= 12; i++)
		pref[i] = pref[i - 1] + m[i];
	for(int i = 1; i <= n; i++)
	{
		cin >> me[i] >> d[i] >> p[i] >> t[i];
	    lay[pref[me[i]] + d[i] + 102] += p[i];
	    lay[pref[me[i]] + d[i] + 102 - t[i] + 1] -= p[i];
	}
	int cur = 0, ans = 0;
	for(int i = 1; i <= 4 * N; i++)
	{
		if(cur += lay[i])
		{
			if(cur < 0)
			{
				ans -= cur;
				cur = 0;
			}

		}

	}
	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <cstring>
#include <iterator>
#include <map>
#include <cstdlib>

#define mp make_pair
#define pb push_back
#define se second
#define fr first

using namespace std;

const int N = 400, inf = (int)1e9;
int n, cnt, pref[N], me[N], d[N], p[N], t[N], lay[5 * N], m[13] = {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	cin >> n;
	for(int i = 1; i <= 12; i++)
		pref[i] = pref[i - 1] + m[i];
	for(int i = 1; i <= n; i++)
	{
		cin >> me[i] >> d[i] >> p[i] >> t[i];
	    lay[pref[me[i]] + d[i] + 103] += p[i];
	    lay[pref[me[i]] + d[i] + 102 - t[i] + 1] -= p[i];
	}
	int cur = 0, ans = 0;
	for(int i = 1; i < 5 * N; i++)
	{
		cur += lay[i];

			if(cur < 0)
			{
				ans -= cur;
				cur = 0;
			}

	}
	cout << ans << endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <fstream>
#include <set>
#include <map>
#include <iomanip>
#include <string>
#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <list>
#include <climits>
#include <cctype>
#include <bitset>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vll;
#define RIP(i, a, b) \
for (int i = int(a); i < int(b); i++)
#define MOD 1000007
#define PI 3.1415926536
#define NIL -1
#define MAX 1000000
#define INF numeric_limits<double>::infinity();
#define ll  unsigned long long

int year[12];
void init_year(){
    year[0] = 31;
    year[1] = 28;
    year[2] = 31;
    year[3] = 30;
    year[4] = 31;
    year[5] = 30;
    year[6] = 31;
    year[7] = 31;
    year[8] = 30;
    year[9] = 31;
    year[10] = 30;
    year[11] = 31;

}

void inc(int & day, int & mon ,int amnt){
    int temp = year[mon-1];
    if(temp >= (day+amnt)){
        day+=amnt;
    }
    else{
        day = (day+1)-temp;
        mon++;
        if(mon > 12)
            mon = 1;
    }
}
void dec(int & day, int & mon ,int amnt){
    if((day-amnt) > 0){
        day-=amnt;
        return;
    }
    else{
        mon--;
        amnt-= day;
        if(mon < 1)
            mon = 12;

         day = year[mon-1];
         dec(day,mon ,amnt);
    }
}

int main()
{
    init_year();
    int n,m,d,p,t;
    cin>>n;
    vector<vector<int> > v(n);
    RIP(i,0,n){
        cin>>m>>d>>p>>t;
        v[i].push_back(m);
        v[i].push_back(d);
        v[i].push_back(p);
        v[i].push_back(t);
        v[i].push_back(0);
        v[i].push_back(0);
    }

    int w = 0 , f = 0 , z = 0, day = 1 , mon = 1 , cnt = 0;
    while(1){
        RIP(i,0,n){

            if(v[i][4] == 0){

            int dd = v[i][1] , mm = v[i][0] , tt = v[i][3];
            dec(dd,mm,tt);
            //cout<<">>  "<<day<<"  "<<mon<<"  /  "<<dd<<"  "<<mm<<endl;
            if(mm > v[i][0]){
                mm = mon;
                dd = day;
            }
            if(((dd) == day)&&(mm == mon)){
                if(v[i][2] <= f){
                    f-=v[i][2];
                }
                else{
                    f = 0;
                }
                w+=v[i][2];
                z++;
                v[i][4] = 1 ;

              //cout<<">>  "<<day<<"  "<<mon<<endl;
              //cout<<w<<"  "<<f<<"  "<<z<<endl;
            }

            }
            if(v[i][5] == 0){
                int dd = v[i][1] , mm = v[i][0];
                dec(dd,mm,1);
                if((dd == day)&&(mm == mon)){
                    f+=v[i][2];
                    w-=v[i][2];
                    z--;
                    cnt++;
                    v[i][5] = 1 ;
                    cnt++;

                  //cout<<">>  "<<day<<"  "<<mon<<endl;
                  //cout<<w<<"  "<<f<<"  "<<z<<endl;
                }
            }
        }
        //cout<<">>  "<<day<<"  "<<mon<<endl;
        //cout<<w<<"  "<<f<<"  "<<z<<"  "<<cnt<<endl;
        inc(day,mon , 1);
        if((day == 31 && mon == 12)|| cnt == n)
            break;
    }

    cout<<f<<endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <fstream>
#include <set>
#include <map>
#include <iomanip>
#include <string>
#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <list>
#include <climits>
#include <cctype>
#include <bitset>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vll;
#define RIP(i, a, b) \
for (int i = int(a); i < int(b); i++)
#define MOD 1000007
#define PI 3.1415926536
#define NIL -1
#define MAX 1000000
#define INF numeric_limits<double>::infinity();
#define ll  unsigned long long

int year[12];
void init_year(){
    year[0] = 31;
    year[1] = 28;
    year[2] = 31;
    year[3] = 30;
    year[4] = 31;
    year[5] = 30;
    year[6] = 31;
    year[7] = 31;
    year[8] = 30;
    year[9] = 31;
    year[10] = 30;
    year[11] = 31;

}

void inc(int & day, int & mon ,int amnt){
    int temp = year[mon-1];
    if(temp >= (day+amnt)){
        day+=amnt;
    }
    else{
        day = (day+1)-temp;
        mon++;
        if(mon > 12)
            mon = 1;
    }
}
void dec(int & day, int & mon ,int amnt){
    if((day-amnt) > 0){
        day-=amnt;
        return;
    }
    else{
        mon--;
        amnt-= day;
        if(mon < 1)
            mon = 12;

         day = year[mon-1];
         dec(day,mon ,amnt);
    }
}

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    init_year();
    int n,m,d,p,t,ret=0;
    cin>>n;
    vector<vector<int> > v(n);
    RIP(i,0,n){
        cin>>m>>d>>p>>t;
        v[i].push_back(m);
        v[i].push_back(d);
        v[i].push_back(p);
        v[i].push_back(t);
        v[i].push_back(0);
        v[i].push_back(0);
    }

    int w = 0 , f = 0 , z = 0, day = 1 , mon = 1 , cnt = 0;
    while(1){
        RIP(i,0,n){

            if(v[i][4] == 0){

            int dd = v[i][1] , mm = v[i][0] , tt = v[i][3];
            dec(dd,mm,tt);
            //cout<<">>  "<<day<<"  "<<mon<<"  /  "<<dd<<"  "<<mm<<endl;
            if(mm > v[i][0]){
                mm = mon;
                dd = day;
            }
            if(((dd) == day)&&(mm == mon)){
                if(v[i][2] <= f){
                    f-=v[i][2];
                    //cout<<"x "<<v[i][1]<<"  "<<v[i][0]<<"  "<<v[i][2]<<"  "<<v[i][3]<<"  "<<f<<"  "<<dd<<"  "<<mm<<endl;
                }
                else{
                    //f = 0;
                    ret+=(v[i][2]-f);
                    //cout<<">>  "<<v[i][1]<<"  "<<v[i][0]<<"  "<<v[i][2]<<"  "<<f<<"   "<<ret<<"  "<<dd<<"  "<<mm<<endl;
                    f = 0;
                }
                w+=v[i][2];
                z++;
                v[i][4] = 1 ;

              //cout<<">>  "<<day<<"  "<<mon<<"    "<<v[i][1]<<"  "<<v[i][0]<<endl;
              //cout<<w<<"  "<<f<<"  "<<z<<endl;
            }

            }
        }
        RIP(i,0,n){
            if(v[i][5] == 0){
                int dd = v[i][1] , mm = v[i][0];
                dec(dd,mm,1);
                if((dd == day)&&(mm == mon)){
                    f+=v[i][2];
                    w-=v[i][2];
                    z--;
                    cnt++;
                    v[i][5] = 1 ;
                    cnt++;

                  //cout<<"--  "<<day<<"  "<<mon<<"  "<<f<<endl;
                  //cout<<"                                       "<<w<<"  "<<f<<"  "<<z<<endl;
                }
            }
        }
        //cout<<">>  "<<day<<"  "<<mon<<endl;
        //cout<<w<<"  "<<f<<"  "<<z<<"  "<<cnt<<endl;
        inc(day,mon , 1);
        if((day == 31 && mon == 12))
            break;
    }

    cout<<max(f,ret)<<endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <fstream>
#include <set>
#include <map>
#include <iomanip>
#include <string>
#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <list>
#include <climits>
#include <cctype>
#include <bitset>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vll;
#define RIP(i, a, b) \
for (int i = int(a); i < int(b); i++)
#define MOD 1000007
#define PI 3.1415926536
#define NIL -1
#define MAX 1000000
#define INF numeric_limits<double>::infinity();
#define ll  unsigned long long

int year[12];
map<int,int>p;
int G[101][101] , V , b = 0;
vector<int> visted(100),cycle;
stack <int> s ,s1;
int cnt = 0;

void containsOdd(int src)
{
    int colorArr[V+1];
    for (int i = 1; i <= V; ++i)
        colorArr[i] = -1;

    // Assign first color to source
    colorArr[src] = 1;

    // Create a queue (FIFO) of vertex numbers and
    // enqueue source vertex for BFS traversal
    queue <int> q;
    q.push(src);

    // Run while there are vertices in queue (Similar to BFS)
    while (!q.empty())
    {
        // Dequeue a vertex from queue
        int u = q.front();
        q.pop();
        //cout<<u<<endl;
        // Find all non-colored adjacent vertices
        for (int v = 1; v <= V; v++)
        {
            // An edge from u to v exists and destination
            // v is not colored
            if ((G[u][v] || G[v][u]) && colorArr[v] == -1)
            {
                // Assign alternate color to this adjacent
                // v of u
                colorArr[v] = 1 - colorArr[u];
                q.push(v);
                //cout<<u<<"  "<<v<<endl;
            }

            // An edge from u to v exists and destination
            // v is colored with same color as u
            else if ((G[u][v] || G[v][u]) && colorArr[v] == colorArr[u]){
                cnt++;
                //cout<<u<<"  "<<v<<endl;
            }
        }
        if(q.empty()){
            for (int v = 1; v <= V; v++){
                if(colorArr[v] == -1){
                    colorArr[v] = 1;
                    q.push(v);
                    break;
                }
            }
        }
    }
}

int nChoosek( int n, int k )
{
    if (k > n) return 0;
    if (k * 2 > n) k = n-k;
    if (k == 0) return 1;

    int result = n;
    for( int i = 2; i <= k; ++i ) {
        result *= (n-i+1);
        result /= i;
    }
    return result;
}

bool conv(string t, int base){
    double h = 0, m = 0 , mi = 0 , hi = 0 , l = 1;
    bool b = 0;
    for(int i = t.size()-1; i >= 0 ; i--){
        l = (t[i]-'0');

        if(t[i] == ':'){
            b = 1;
            continue;
        }

        if((int)t[i] > 64){
            l-=7;
        }

        if((t[i]!= ':') && (l >= base)){
            return false;
        }

        if(b == 0){
            double mm = (l)*pow(base ,mi);
            m+=mm;
            mi++;
        }
        else{
            double hh = ((l)*pow(base ,hi));
            h+=hh;
            //cout<<"hh:  "<<hh<<endl;
            hi++;
        }
    }
    //cout<<">>  "<<base<<"  "<<h<<"  "<<m<<endl;
    if( (h < 24.0) && (m < 60.0)) return true;

    return false;
}

void init_year(){
    year[0] = 31;
    year[1] = 28;
    year[2] = 31;
    year[3] = 30;
    year[4] = 31;
    year[5] = 30;
    year[6] = 31;
    year[7] = 31;
    year[8] = 30;
    year[9] = 31;
    year[10] = 30;
    year[11] = 31;

}

void inc(int & day, int & mon ,int amnt){
    int temp = year[mon-1];
    if(temp >= (day+amnt)){
        day+=amnt;
    }
    else{
        day = (day+1)-temp;
        mon++;
        if(mon > 12)
            mon = 1;
    }
}
void dec(int & day, int & mon ,int amnt){
    if((day-amnt) > 0){
        day-=amnt;
        return;
    }
    else{
        mon--;
        amnt-= day;
        if(mon < 1)
            mon = 12;

         day = year[mon-1];
         dec(day,mon ,amnt);
        //int temp = year[mon-1];
        //day = temp;
    }
}
struct olyp {
   int m,d,p,t,s,e;
   olyp(int m,int d , int p , int t , int s , int e) {
      this -> m = m;
      this -> d = d;
      this -> p = p;
      this -> t = t;
      this -> s = s;
      this -> e = e;
   }
};

int main()
{
    init_year();
    int n,m,d,p,t;
    vector<olyp> v;
    cin>>n;
    RIP(i,0,n){
        cin>>m>>d>>p>>t;
        v.push_back(olyp(m,d,p,t,0,0));
    }

    /*sort(v.begin(), v.end(),
          [](const olyp & a, const olyp & b) {
                if(a.m < b.m)
                    return true;
                else if(a.d < b.d)
                    return true;
                else
                    return false;
            }
    );*/
    int w = 0 , f = 0 , z = 0, day = 1 , mon = 1 , cnt = 0;
    while(1){
        RIP(i,0,n){

            if(v[i].s == 0){

            int dd = v[i].d , mm = v[i].m , tt = v[i].t;
            dec(dd,mm,tt);
            if(mm > v[i].m){
                mm = mon;
                dd = day;
            }
            if(((dd) == day)&&(mm == mon)){
                if(v[i].p <= f){
                    f-=v[i].p;
                }
                else{
                    f = 0;
                }
                w+=v[i].p;
                z++;
                v[i].s = 1 ;
            }
              //cout<<">>  "<<day<<"  "<<mon<<endl;
              //cout<<w<<"  "<<f<<"  "<<z<<endl;

            }
            if(v[i].e == 0){
                int dd = v[i].d , mm = v[i].m;
                dec(dd,mm,1);
                if((dd == day)&&(mm == mon)){
                    f+=v[i].p;
                    w-=v[i].p;
                    z--;
                    cnt++;
                    v[i].e = 1 ;
                }
                  //cout<<">>  "<<day<<"  "<<mon<<endl;
                  //cout<<w<<"  "<<f<<"  "<<z<<endl;
            }
        }
        //cout<<">>  "<<day<<"  "<<mon<<endl;
        //cout<<w<<"  "<<f<<"  "<<z<<endl;
        inc(day,mon , 1);
        if(day == 31 && mon == 12)
            break;
    }

    cout<<f<<endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <fstream>
#include <set>
#include <map>
#include <iomanip>
#include <string>
#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <list>
#include <climits>
#include <cctype>
#include <bitset>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vll;
#define RIP(i, a, b) \
for (int i = int(a); i < int(b); i++)
#define MOD 1000007
#define PI 3.1415926536
#define NIL -1
#define MAX 1000000
#define INF numeric_limits<double>::infinity();
#define ll  unsigned long long

int year[12];
void init_year(){
    year[0] = 31;
    year[1] = 28;
    year[2] = 31;
    year[3] = 30;
    year[4] = 31;
    year[5] = 30;
    year[6] = 31;
    year[7] = 31;
    year[8] = 30;
    year[9] = 31;
    year[10] = 30;
    year[11] = 31;

}

void inc(int & day, int & mon ,int amnt){
    int temp = year[mon-1];
    if(temp >= (day+amnt)){
        day+=amnt;
    }
    else{
        day = (day+1)-temp;
        mon++;
        if(mon > 12)
            mon = 1;
    }
}
void dec(int & day, int & mon ,int amnt){
    if((day-amnt) > 0){
        day-=amnt;
        return;
    }
    else{
        mon--;
        amnt-= day;
        if(mon < 1)
            mon = 12;

         day = year[mon-1];
         dec(day,mon ,amnt);
    }
}

int main()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    init_year();
    int n,m,d,p,t,ret=0;
    cin>>n;
    vector<vector<int> > v(n);
    RIP(i,0,n){
        cin>>m>>d>>p>>t;
        v[i].push_back(m);
        v[i].push_back(d);
        v[i].push_back(p);
        v[i].push_back(t);
        v[i].push_back(0);
        v[i].push_back(0);
    }

    int w = 0 , f = 0 , z = 0, day = 1 , mon = 1 , cnt = 0;
    while(1){
        RIP(i,0,n){

            if(v[i][4] == 0){

            int dd = v[i][1] , mm = v[i][0] , tt = v[i][3];
            dec(dd,mm,tt);
            //cout<<">>  "<<day<<"  "<<mon<<"  /  "<<dd<<"  "<<mm<<endl;
            if(mm > v[i][0]){
                mm = mon;
                dd = day;
            }
            if(((dd) == day)&&(mm == mon)){
                if(v[i][2] <= f){
                    f-=v[i][2];
                    //cout<<"x "<<v[i][1]<<"  "<<v[i][0]<<"  "<<v[i][2]<<"  "<<v[i][3]<<"  "<<f<<"  "<<dd<<"  "<<mm<<endl;
                }
                else{
                    //f = 0;
                    ret+=(v[i][2]-f);
                    //cout<<">>  "<<v[i][1]<<"  "<<v[i][0]<<"  "<<v[i][2]<<"  "<<f<<"   "<<ret<<"  "<<dd<<"  "<<mm<<endl;
                    f = 0;
                }
                w+=v[i][2];
                z++;
                v[i][4] = 1 ;

              //cout<<">>  "<<day<<"  "<<mon<<"    "<<v[i][1]<<"  "<<v[i][0]<<endl;
              //cout<<w<<"  "<<f<<"  "<<z<<endl;
            }

            }
        }
        RIP(i,0,n){
            if(v[i][5] == 0){
                int dd = v[i][1] , mm = v[i][0];
                dec(dd,mm,1);
                if((dd == day)&&(mm == mon)){
                    f+=v[i][2];
                    w-=v[i][2];
                    z--;
                    cnt++;
                    v[i][5] = 1 ;
                    cnt++;

                  //cout<<"--  "<<day<<"  "<<mon<<"  "<<f<<endl;
                  //cout<<"                                       "<<w<<"  "<<f<<"  "<<z<<endl;
                }
            }
        }
        //cout<<">>  "<<day<<"  "<<mon<<endl;
        //cout<<w<<"  "<<f<<"  "<<z<<"  "<<cnt<<endl;
        inc(day,mon , 1);
        if((day == 31 && mon == 12))
            break;
    }

    cout<<f<<endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <fstream>
#include <set>
#include <map>
#include <iomanip>
#include <string>
#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <list>
#include <climits>
#include <cctype>
#include <bitset>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vll;
#define RIP(i, a, b) \
for (int i = int(a); i < int(b); i++)
#define MOD 1000007
#define PI 3.1415926536
#define NIL -1
#define MAX 1000000
#define INF numeric_limits<double>::infinity();
#define ll  unsigned long long

int year[12];
map<int,int>p;
int G[101][101] , V , b = 0;
vector<int> visted(100),cycle;
stack <int> s ,s1;
int cnt = 0;

void containsOdd(int src)
{
    int colorArr[V+1];
    for (int i = 1; i <= V; ++i)
        colorArr[i] = -1;

    // Assign first color to source
    colorArr[src] = 1;

    // Create a queue (FIFO) of vertex numbers and
    // enqueue source vertex for BFS traversal
    queue <int> q;
    q.push(src);

    // Run while there are vertices in queue (Similar to BFS)
    while (!q.empty())
    {
        // Dequeue a vertex from queue
        int u = q.front();
        q.pop();
        //cout<<u<<endl;
        // Find all non-colored adjacent vertices
        for (int v = 1; v <= V; v++)
        {
            // An edge from u to v exists and destination
            // v is not colored
            if ((G[u][v] || G[v][u]) && colorArr[v] == -1)
            {
                // Assign alternate color to this adjacent
                // v of u
                colorArr[v] = 1 - colorArr[u];
                q.push(v);
                //cout<<u<<"  "<<v<<endl;
            }

            // An edge from u to v exists and destination
            // v is colored with same color as u
            else if ((G[u][v] || G[v][u]) && colorArr[v] == colorArr[u]){
                cnt++;
                //cout<<u<<"  "<<v<<endl;
            }
        }
        if(q.empty()){
            for (int v = 1; v <= V; v++){
                if(colorArr[v] == -1){
                    colorArr[v] = 1;
                    q.push(v);
                    break;
                }
            }
        }
    }
}

int nChoosek( int n, int k )
{
    if (k > n) return 0;
    if (k * 2 > n) k = n-k;
    if (k == 0) return 1;

    int result = n;
    for( int i = 2; i <= k; ++i ) {
        result *= (n-i+1);
        result /= i;
    }
    return result;
}

bool conv(string t, int base){
    double h = 0, m = 0 , mi = 0 , hi = 0 , l = 1;
    bool b = 0;
    for(int i = t.size()-1; i >= 0 ; i--){
        l = (t[i]-'0');

        if(t[i] == ':'){
            b = 1;
            continue;
        }

        if((int)t[i] > 64){
            l-=7;
        }

        if((t[i]!= ':') && (l >= base)){
            return false;
        }

        if(b == 0){
            double mm = (l)*pow(base ,mi);
            m+=mm;
            mi++;
        }
        else{
            double hh = ((l)*pow(base ,hi));
            h+=hh;
            //cout<<"hh:  "<<hh<<endl;
            hi++;
        }
    }
    //cout<<">>  "<<base<<"  "<<h<<"  "<<m<<endl;
    if( (h < 24.0) && (m < 60.0)) return true;

    return false;
}

void init_year(){
    year[0] = 31;
    year[1] = 28;
    year[2] = 31;
    year[3] = 30;
    year[4] = 31;
    year[5] = 30;
    year[6] = 31;
    year[7] = 31;
    year[8] = 30;
    year[9] = 31;
    year[10] = 30;
    year[11] = 31;

}

void inc(int & day, int & mon ,int amnt){
    int temp = year[mon-1];
    if(temp >= (day+amnt)){
        day+=amnt;
    }
    else{
        day = (day+1)-temp;
        mon++;
        if(mon > 12)
            mon = 1;
    }
}
void dec(int & day, int & mon ,int amnt){
    if((day-amnt) > 0){
        day-=amnt;
        return;
    }
    else{
        mon--;
        amnt-= day;
        if(mon < 1)
            mon = 12;

         day = year[mon-1];
         dec(day,mon ,amnt);
        //int temp = year[mon-1];
        //day = temp;
    }
}
struct olyp {
   int m,d,p,t,s,e;
   olyp(int m,int d , int p , int t , int s , int e) {
      this -> m = m;
      this -> d = d;
      this -> p = p;
      this -> t = t;
      this -> s = s;
      this -> e = e;
   }
};

int main()
{
    init_year();
    int n,m,d,p,t;
    vector<olyp> v;
    cin>>n;
    RIP(i,0,n){
        cin>>m>>d>>p>>t;
        v.push_back(olyp(m,d,p,t,0,0));
    }

    /*sort(v.begin(), v.end(),
          [](const olyp & a, const olyp & b) {
                if(a.m < b.m)
                    return true;
                else if(a.d < b.d)
                    return true;
                else
                    return false;
            }
    );*/
    int w = 0 , f = 0 , z = 0, day = 1 , mon = 1 , cnt = 0;
    while(1){
        RIP(i,0,n){

            if(v[i].s == 0){

            int dd = v[i].d , mm = v[i].m , tt = v[i].t;
            dec(dd,mm,tt);
            if(mm > v[i].m){
                mm = mon;
                dd = day;
            }
            if(((dd) == day)&&(mm == mon)){
                if(v[i].p <= f){
                    f-=v[i].p;
                }
                else{
                    f = 0;
                }
                w+=v[i].p;
                z++;
                v[i].s = 1 ;
            }
              //cout<<">>  "<<day<<"  "<<mon<<endl;
              //cout<<w<<"  "<<f<<"  "<<z<<endl;

            }
            if(v[i].e == 0){
                int dd = v[i].d , mm = v[i].m;
                dec(dd,mm,1);
                if((dd == day)&&(mm == mon)){
                    f+=v[i].p;
                    w-=v[i].p;
                    z--;
                    cnt++;
                    v[i].e = 1 ;
                    cnt++;
                }
                  //cout<<">>  "<<day<<"  "<<mon<<endl;
                  //cout<<w<<"  "<<f<<"  "<<z<<endl;
            }
        }
        //cout<<">>  "<<day<<"  "<<mon<<endl;
        //cout<<w<<"  "<<f<<"  "<<z<<endl;
        inc(day,mon , 1);
        if((day == 31 && mon == 12)|| cnt == n)
            break;
    }

    cout<<f<<endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <fstream>
#include <set>
#include <map>
#include <iomanip>
#include <string>
#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <list>
#include <climits>
#include <cctype>
#include <bitset>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vll;
#define RIP(i, a, b) \
for (int i = int(a); i < int(b); i++)
#define MOD 1000007
#define PI 3.1415926536
#define NIL -1
#define MAX 1000000
#define INF numeric_limits<double>::infinity();
#define ll  unsigned long long

int year[12];
map<int,int>p;
int G[101][101] , V , b = 0;
vector<int> visted(100),cycle;
stack <int> s ,s1;
int cnt = 0;

void containsOdd(int src)
{
    int colorArr[V+1];
    for (int i = 1; i <= V; ++i)
        colorArr[i] = -1;

    // Assign first color to source
    colorArr[src] = 1;

    // Create a queue (FIFO) of vertex numbers and
    // enqueue source vertex for BFS traversal
    queue <int> q;
    q.push(src);

    // Run while there are vertices in queue (Similar to BFS)
    while (!q.empty())
    {
        // Dequeue a vertex from queue
        int u = q.front();
        q.pop();
        //cout<<u<<endl;
        // Find all non-colored adjacent vertices
        for (int v = 1; v <= V; v++)
        {
            // An edge from u to v exists and destination
            // v is not colored
            if ((G[u][v] || G[v][u]) && colorArr[v] == -1)
            {
                // Assign alternate color to this adjacent
                // v of u
                colorArr[v] = 1 - colorArr[u];
                q.push(v);
                //cout<<u<<"  "<<v<<endl;
            }

            // An edge from u to v exists and destination
            // v is colored with same color as u
            else if ((G[u][v] || G[v][u]) && colorArr[v] == colorArr[u]){
                cnt++;
                //cout<<u<<"  "<<v<<endl;
            }
        }
        if(q.empty()){
            for (int v = 1; v <= V; v++){
                if(colorArr[v] == -1){
                    colorArr[v] = 1;
                    q.push(v);
                    break;
                }
            }
        }
    }
}

int nChoosek( int n, int k )
{
    if (k > n) return 0;
    if (k * 2 > n) k = n-k;
    if (k == 0) return 1;

    int result = n;
    for( int i = 2; i <= k; ++i ) {
        result *= (n-i+1);
        result /= i;
    }
    return result;
}

bool conv(string t, int base){
    double h = 0, m = 0 , mi = 0 , hi = 0 , l = 1;
    bool b = 0;
    for(int i = t.size()-1; i >= 0 ; i--){
        l = (t[i]-'0');

        if(t[i] == ':'){
            b = 1;
            continue;
        }

        if((int)t[i] > 64){
            l-=7;
        }

        if((t[i]!= ':') && (l >= base)){
            return false;
        }

        if(b == 0){
            double mm = (l)*pow(base ,mi);
            m+=mm;
            mi++;
        }
        else{
            double hh = ((l)*pow(base ,hi));
            h+=hh;
            //cout<<"hh:  "<<hh<<endl;
            hi++;
        }
    }
    //cout<<">>  "<<base<<"  "<<h<<"  "<<m<<endl;
    if( (h < 24.0) && (m < 60.0)) return true;

    return false;
}

void init_year(){
    year[0] = 31;
    year[1] = 28;
    year[2] = 31;
    year[3] = 30;
    year[4] = 31;
    year[5] = 30;
    year[6] = 31;
    year[7] = 31;
    year[8] = 30;
    year[9] = 31;
    year[10] = 30;
    year[11] = 31;

}

void inc(int & day, int & mon ,int amnt){
    int temp = year[mon-1];
    if(temp >= (day+amnt)){
        day+=amnt;
    }
    else{
        day = (day+1)-temp;
        mon++;
        if(mon > 12)
            mon = 1;
    }
}
void dec(int & day, int & mon ,int amnt){
    if((day-amnt) > 0){
        day-=amnt;
    }
    else{

        mon--;
        if(mon < 1)
            mon = 12;

        int temp = year[mon-1];
        day = temp;
    }
}
struct olyp {
   int m,d,p,t;
   olyp(int m,int d , int p , int t) {
      this -> m = m;
      this -> d = d;
      this -> p = p;
      this -> t = t;
   }
};

int main()
{
    init_year();
    int n,m,d,p,t;
    vector<olyp> v;
    cin>>n;
    RIP(i,0,n){
        cin>>m>>d>>p>>t;
        v.push_back(olyp(m,d,p,t));
    }

    /*sort(v.begin(), v.end(),
          [](const olyp & a, const olyp & b) {
                if(a.m < b.m)
                    return true;
                else if(a.d < b.d)
                    return true;
                else
                    return false;
            }
    );*/
    int w = 0 , f = 0 , z = 0, day = 1 , mon = 1 , cnt = 0;
    while(1){
        RIP(i,0,n){
            int dd = v[i].d , mm = v[i].m , tt = v[i].t;
            dec(dd,mm,tt);
            if(((v[i].d-v[i].t) == day)&&(v[i].m == mon)){
                if(v[i].p <= f){
                    f-=v[i].p;
                }
                else{
                    f = 0;
                }
                w+=v[i].p;
                z++;
            }
            {
                int dd = v[i].d , mm = v[i].m;
                dec(dd,mm,1);
                if((dd == day)&&(mm == mon)){
                    f+=v[i].p;
                    w-=v[i].p;
                    z--;
                    cnt++;
                }
            }
        }
        cout<<">>  "<<day<<"  "<<mon<<endl;
        cout<<w<<"  "<<f<<"  "<<z<<endl;
        inc(day,mon , 1);
        if(day == 31 && mon == 12)
            break;
    }

    cout<<f<<endl;

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <fstream>
#include <set>
#include <map>
#include <iomanip>
#include <string>
#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <list>
#include <climits>
#include <cctype>
#include <bitset>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vll;
#define RIP(i, a, b) \
for (int i = int(a); i < int(b); i++)
#define MOD 1000007
#define PI 3.1415926536
#define NIL -1
#define MAX 1000000
#define INF numeric_limits<double>::infinity();
#define ll  unsigned long long

int year[12];
void init_year(){
    year[0] = 31;
    year[1] = 28;
    year[2] = 31;
    year[3] = 30;
    year[4] = 31;
    year[5] = 30;
    year[6] = 31;
    year[7] = 31;
    year[8] = 30;
    year[9] = 31;
    year[10] = 30;
    year[11] = 31;

}

void inc(int & day, int & mon ,int amnt){
    int temp = year[mon-1];
    if(temp >= (day+amnt)){
        day+=amnt;
    }
    else{
        day = (day+1)-temp;
        mon++;
        if(mon > 12)
            mon = 1;
    }
}
void dec(int & day, int & mon ,int amnt){
    if((day-amnt) > 0){
        day-=amnt;
        return;
    }
    else{
        mon--;
        amnt-= day;
        if(mon < 1)
            mon = 12;

         day = year[mon-1];
         dec(day,mon ,amnt);
    }
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    init_year();
    int n,m,d,p,t,ret=0;
    cin>>n;
    vector<vector<int> > v(n);
    RIP(i,0,n){
        cin>>m>>d>>p>>t;
        v[i].push_back(m);
        v[i].push_back(d);
        v[i].push_back(p);
        v[i].push_back(t);
        v[i].push_back(0);
        v[i].push_back(0);
    }

    int w = 0 , f = 0 , z = 0, day = 1 , mon = 1 , cnt = 0;
    while(1){
        RIP(i,0,n){

            if(v[i][4] == 0){

            int dd = v[i][1] , mm = v[i][0] , tt = v[i][3];
            dec(dd,mm,tt);
            //cout<<">>  "<<day<<"  "<<mon<<"  /  "<<dd<<"  "<<mm<<endl;
            if(mm > v[i][0]){
                mm = mon;
                dd = day;
            }
            if(((dd) == day)&&(mm == mon)){
                if(v[i][2] <= f){
                    f-=v[i][2];
                    //cout<<"x "<<v[i][1]<<"  "<<v[i][0]<<"  "<<v[i][2]<<"  "<<v[i][3]<<"  "<<f<<"  "<<dd<<"  "<<mm<<endl;
                }
                else{
                    //f = 0;
                    ret+=(v[i][2]-f);
                    //cout<<">>  "<<v[i][1]<<"  "<<v[i][0]<<"  "<<v[i][2]<<"  "<<f<<"   "<<ret<<"  "<<dd<<"  "<<mm<<endl;
                    f = 0;
                }
                w+=v[i][2];
                z++;
                v[i][4] = 1 ;

              //cout<<">>  "<<day<<"  "<<mon<<"    "<<v[i][1]<<"  "<<v[i][0]<<endl;
              //cout<<w<<"  "<<f<<"  "<<z<<endl;
            }

            }
        }
        RIP(i,0,n){
            if(v[i][5] == 0){
                int dd = v[i][1] , mm = v[i][0];
                dec(dd,mm,1);
                if((dd == day)&&(mm == mon)){
                    f+=v[i][2];
                    w-=v[i][2];
                    z--;
                    cnt++;
                    v[i][5] = 1 ;
                    cnt++;

                  //cout<<"--  "<<day<<"  "<<mon<<"  "<<f<<endl;
                  //cout<<"                                       "<<w<<"  "<<f<<"  "<<z<<endl;
                }
            }
        }
        //cout<<">>  "<<day<<"  "<<mon<<endl;
        //cout<<w<<"  "<<f<<"  "<<z<<"  "<<cnt<<endl;
        inc(day,mon , 1);
        if((day == 31 && mon == 12))
            break;
    }

    cout<<ret<<endl;

    return 0;
}


/*
    author:  Maksim1744
    created: 18.08.2020 23:51:33
*/

#include <bits/stdc++.h>

using namespace std;

#define ll   long long
#define ld   long double

#define mp   make_pair
#define pb   push_back
#define eb   emplace_back

#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template<typename T>             vector<T>& operator--            (vector<T>& v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T>& v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T>& v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>& v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p){is >> p.first >> p.second;        return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>& p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

#ifdef HOME
#define TAG_LENGTH       25
#define LR_LEFT          left
#define LR_RIGHT         right
#define LR_VALUE         value
#define LR_SECOND_VALUE  mn
#include "C:/C++ libs/print.cpp"
#else
#define showl            42;
#define shows            42;
#define show(...)        42;
#define showm(...)       42;
#define showt(...)       42;
#define printTree(...)   42;
#define printGraph(...)  42;
#define printLRTree(...) 42;
#define printMatrix(...) 42;
#define printWGraph(...) 42;
#define debug     if (false)
#endif

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    int n, k;
    cin >> n >> k;
    vector<vector<int>> v(4, vector<int>(n));
    cin >> v;
    if (n == 1 && k == 2) {
        if (v[1][0] == v[0][0]) {
            cout << 2 << '\n';
            cout << "1 1 1\n";
            cout << "2 4 1\n";
        } else {
            cout << -1 << '\n';
        }
        return 0;
    }

    vector<pair<int, pair<int, int>>> ans;

    auto spin = [&]() {
        int x = ans.size();
        for (int i = 0; i < n - 1; ++i) {
            if (v[1][i + 1] == 0 && v[1][i] != 0) {
                ans.eb(v[1][i], mp(1, i + 1));
            }
        }
        if (v[2][n - 1] == 0 && v[1][n - 1] != 0) {
            ans.eb(v[1][n - 1], mp(2, n - 1));
        }
        for (int i = 1; i < n; ++i) {
            if (v[2][i - 1] == 0 && v[2][i] != 0) {
                ans.eb(v[2][i], mp(2, i - 1));
            }
        }
        if (v[1][0] == 0 && v[2][0] != 0) {
            ans.eb(v[2][0], mp(1, 0));
        }
        for (; x < ans.size(); ++x) {
            for (int i = 1; i < 3; ++i) {
                for (int j = 0; j < n; ++j) {
                    if (v[i][j] == ans[x].first) {
                        v[i][j] = 0;
                    }
                }
            }
            v[ans[x].second.first][ans[x].second.second] = ans[x].first;
        }
    };

    auto place = [&]() {
        int done = 0;
        for (int i = 0; i < n; ++i) {
            if (v[0][i] == v[1][i] && v[0][i] != 0) {
                ans.eb(v[0][i], mp(0, i));
                v[1][i] = 0;
                v[0][i] = -1;
                ++done;
            }
            if (v[2][i] == v[3][i] && v[2][i] != 0) {
                ans.eb(v[2][i], mp(3, i));
                v[2][i] = 0;
                v[3][i] = -1;
                ++done;
            }
        }
        return done;
    };

    if (k == n * 2) {
        int done = place();
        if (done == 0) {
            cout << -1 << '\n';
            return 0;
        }
        while (done < k) {
            spin();
            done += place();
        }
        // int p1 = v[0][0];
        // assert(p1 != 0);
        // int c1 = v[1][0];
        // ans.eb(v[1][0], mp(0, 0));
        // v[1][0] = 0;
        // v[0][0] = -1;
        // int done = 0;
        // while (done == 0) {
        //     done += place();
        //     spin();
        // }
        // while (v[1][0] != 0)
        //     spin();
        // ans.eb(c1, mp(1, 0));
        // v[0][0] = p1;
        // v[1][0] = c1;
        // while (done < k) {
        //     done += place();
        //     spin();
        // }
    } else {
        int done = 0;
        while (done < k) {
            done += place();
            spin();
        }
    }

    assert(ans.size() <= 20000);
    cout << ans.size() << '\n';
    for (auto [x, p] : ans) {
        cout << x << ' ' << p.first + 1 << ' ' << p.second + 1 << '\n';
    }

    return 0;
}


/*
    author:  Maksim1744
    created: 18.08.2020 23:51:33
*/

#include <bits/stdc++.h>

using namespace std;

#define ll   long long
#define ld   long double

#define mp   make_pair
#define pb   push_back
#define eb   emplace_back

#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template<typename T>             vector<T>& operator--            (vector<T>& v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T>& v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T>& v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>& v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p){is >> p.first >> p.second;        return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>& p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

#ifdef HOME
#define TAG_LENGTH       25
#define LR_LEFT          left
#define LR_RIGHT         right
#define LR_VALUE         value
#define LR_SECOND_VALUE  mn
#include "C:/C++ libs/print.cpp"
#else
#define showl            42;
#define shows            42;
#define show(...)        42;
#define showm(...)       42;
#define showt(...)       42;
#define printTree(...)   42;
#define printGraph(...)  42;
#define printLRTree(...) 42;
#define printMatrix(...) 42;
#define printWGraph(...) 42;
#define debug     if (false)
#endif

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    int n, k;
    cin >> n >> k;
    vector<vector<int>> v(4, vector<int>(n));
    cin >> v;
    if (n == 1 && k == 2) {
        if (v[1][0] == v[0][0]) {
            cout << 2 << '\n';
            cout << "1 1 1\n";
            cout << "2 4 1\n";
        } else {
            cout << -1 << '\n';
        }
        return 0;
    }

    vector<pair<int, pair<int, int>>> ans;

    auto spin = [&]() {
        int x = ans.size();
        for (int i = 0; i < n - 1; ++i) {
            if (v[1][i + 1] == 0 && v[1][i] != 0) {
                ans.eb(v[1][i], mp(1, i + 1));
            }
        }
        if (v[2][n - 1] == 0 && v[1][n - 1] != 0) {
            ans.eb(v[1][n - 1], mp(2, n - 1));
        }
        for (int i = 1; i < n; ++i) {
            if (v[2][i - 1] == 0 && v[2][i] != 0) {
                ans.eb(v[2][i], mp(2, i - 1));
            }
        }
        if (v[1][0] == 0 && v[2][0] != 0) {
            ans.eb(v[2][0], mp(1, 0));
        }
        for (; x < ans.size(); ++x) {
            for (int i = 1; i < 3; ++i) {
                for (int j = 0; j < n; ++j) {
                    if (v[i][j] == ans[x].first) {
                        v[i][j] = 0;
                    }
                }
            }
            v[ans[x].second.first][ans[x].second.second] = ans[x].first;
        }
    };

    auto place = [&]() {
        int done = 0;
        for (int i = 0; i < n; ++i) {
            if (v[0][i] == v[1][i] && v[0][i] != 0) {
                ans.eb(v[0][i], mp(0, i));
                v[1][i] = 0;
                v[0][i] = -1;
                ++done;
            }
            if (v[2][i] == v[3][i] && v[2][i] != 0) {
                ans.eb(v[2][i], mp(3, i));
                v[2][i] = 0;
                v[3][i] = -1;
                ++done;
            }
        }
        return done;
    };

    if (k == n * 2) {
        place();
        if (place() != k * 2) {
            cout << -1 << '\n';
            return 0;
        }
        // int p1 = v[0][0];
        // assert(p1 != 0);
        // int c1 = v[1][0];
        // ans.eb(v[1][0], mp(0, 0));
        // v[1][0] = 0;
        // v[0][0] = -1;
        // int done = 0;
        // while (done == 0) {
        //     done += place();
        //     spin();
        // }
        // while (v[1][0] != 0)
        //     spin();
        // ans.eb(c1, mp(1, 0));
        // v[0][0] = p1;
        // v[1][0] = c1;
        // while (done < k) {
        //     done += place();
        //     spin();
        // }
    } else {
        int done = 0;
        while (done < k) {
            done += place();
            spin();
        }
    }

    assert(ans.size() <= 20000);
    cout << ans.size() << '\n';
    for (auto [x, p] : ans) {
        cout << x << ' ' << p.first + 1 << ' ' << p.second + 1 << '\n';
    }

    return 0;
}


/*
    author:  Maksim1744
    created: 18.08.2020 23:51:33
*/

#include <bits/stdc++.h>

using namespace std;

#define ll   long long
#define ld   long double

#define mp   make_pair
#define pb   push_back
#define eb   emplace_back

#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template<typename T>             vector<T>& operator--            (vector<T>& v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T>& v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T>& v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>& v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p){is >> p.first >> p.second;        return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>& p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

#ifdef HOME
#define TAG_LENGTH       25
#define LR_LEFT          left
#define LR_RIGHT         right
#define LR_VALUE         value
#define LR_SECOND_VALUE  mn
#include "C:/C++ libs/print.cpp"
#else
#define showl            42;
#define shows            42;
#define show(...)        42;
#define showm(...)       42;
#define showt(...)       42;
#define printTree(...)   42;
#define printGraph(...)  42;
#define printLRTree(...) 42;
#define printMatrix(...) 42;
#define printWGraph(...) 42;
#define debug     if (false)
#endif

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    int n, k;
    cin >> n >> k;
    vector<vector<int>> v(4, vector<int>(n));
    cin >> v;
    if (n == 1 && k == 2) {
        if (v[1][0] == v[0][0]) {
            cout << 2 << '\n';
            cout << "1 1 1\n";
            cout << "2 4 1\n";
        } else {
            cout << -1 << '\n';
        }
        return 0;
    }

    vector<pair<int, pair<int, int>>> ans;

    auto spin = [&]() {
        int x = ans.size();
        for (int i = 0; i < n - 1; ++i) {
            if (v[1][i + 1] == 0 && v[1][i] != 0) {
                ans.eb(v[1][i], mp(1, i + 1));
            }
        }
        if (v[2][n - 1] == 0 && v[1][n - 1] != 0) {
            ans.eb(v[1][n - 1], mp(2, n - 1));
        }
        for (int i = 1; i < n; ++i) {
            if (v[2][i - 1] == 0 && v[2][i] != 0) {
                ans.eb(v[2][i], mp(2, i - 1));
            }
        }
        if (v[1][0] == 0 && v[2][0] != 0) {
            ans.eb(v[2][0], mp(1, 0));
        }
        for (; x < ans.size(); ++x) {
            for (int i = 1; i < 3; ++i) {
                for (int j = 0; j < n; ++j) {
                    if (v[i][j] == ans[x].first) {
                        v[i][j] = 0;
                    }
                }
            }
            v[ans[x].second.first][ans[x].second.second] = ans[x].first;
        }
    };

    if (k == n * 2) {
        int p1 = v[0][0];
        int c1 = v[1][0];
        ans.eb(v[1][0], mp(0, 0));
        v[1][0] = 0;
        v[0][0] = -1;
        int done = 0;
        while (done < k - 2) {
            for (int i = 0; i < n; ++i) {
                if (v[0][i] == v[1][i] && v[0][i] != 0) {
                    ans.eb(v[0][i], mp(0, i));
                    v[1][i] = 0;
                    ++done;
                }
                if (v[2][i] == v[3][i] && v[2][i] != 0) {
                    ans.eb(v[2][i], mp(3, i));
                    v[2][i] = 0;
                    ++done;
                }
            }
            spin();
        }
        while (v[1][0] != 0)
            spin();
        ans.eb(c1, mp(1, 0));
        v[0][0] = p1;
        v[1][0] = c1;
        while (done < k) {
            for (int i = 0; i < n; ++i) {
                if (v[0][i] == v[1][i] && v[0][i] != 0) {
                    ans.eb(v[0][i], mp(0, i));
                    v[1][i] = 0;
                    ++done;
                }
                if (v[2][i] == v[3][i] && v[2][i] != 0) {
                    ans.eb(v[2][i], mp(3, i));
                    v[2][i] = 0;
                    ++done;
                }
            }
            spin();
        }
    } else {
        int done = 0;
        while (done < k) {
            for (int i = 0; i < n; ++i) {
                if (v[0][i] == v[1][i] && v[0][i] != 0) {
                    ans.eb(v[0][i], mp(0, i));
                    v[1][i] = 0;
                    ++done;
                }
                if (v[2][i] == v[3][i] && v[2][i] != 0) {
                    ans.eb(v[2][i], mp(3, i));
                    v[2][i] = 0;
                    ++done;
                }
            }
            spin();
            printMatrix(v);
            shows;
        }
    }

    assert(ans.size() <= 20000);
    cout << ans.size() << '\n';
    for (auto [n, p] : ans) {
        cout << n << ' ' << p.first + 1 << ' ' << p.second + 1 << '\n';
    }

    return 0;
}