#include<bits/stdc++.h>
using namespace std;
const int A=100000010;
int M[A];
int N[A];
int SM[A];
int SN[A];
long long cnt;
long long cut;
int main(){
int a,b,c,d,n,m;
cin>>a;
for(b=0;b<a;b++){
cin>>c;
if(c%2==0){
for(d=0;d<1;d++){
cin>>M[d]>>SM[d];
cnt+=M[d];
}
}
if(c%2==1){
for(d=0;d<1;d++){
cin>>N[d]>>SN[d];
if(N[d]<=M[d]&&SN[d]-SM[d]<=45) break;
if(N[d]>M[d]||SN[d]-SM[d]>45) cut+=N[d];
}
}
}
cout<<cnt+cut;
return 0;
}