#include<iostream>
#define rep(i,n) for(int i=0;i<n;i++)
#define MH 100
using namespace std;
bool ck(int *f){
	rep(i,5)if(!f[i])return 0;
	return 1;
}
int main(){
	int n;
	while(cin>>n,n){
		int d,l,x,y,c=0;
		int field[MH][5]={0};
		rep(i,n){
			cin>>d>>l>>x,y=MH,x--,d--;
			for(;y>0;y--)rep(j,(d?1:l))if(field[y-1][x+j])goto END;
			END:
			rep(j,l)field[y+d*j][x+(1-d)*j]=1;
			rep(i,MH)while(ck(field[i])){
				rep(j,MH-i-1)rep(k,5)field[j+i][k]=field[j+i+1][k];
				rep(k,5)field[MH-1][k]=0;
			}
		}
		rep(i,MH)rep(j,5)c+=field[i][j];
		cout<<c<<endl;
	}
	return 0;
}