#include <iostream>
#include <cstring>
using namespace std;

int t[5000][5];

void destroy(void){
	for(int i=4999;i>=0;i--){
		int count = 0;
		for(int j=0;j<5;j++){
			count += t[i][j];
		}

		if(count == 0) break;

		else if(count == 5){
			for(int k=i;k>0;k--){
				count = 0;
				for(int j=0;j<5;j++){
					t[k][j] = t[k-1][j];
					t[k-1][j] = 0;
					count += t[k][j];
				}
				if(count == 0) break;
			}
			i--;
		}
	}
}

void setBlock(int d,int p,int q){
	int sj = q - 1;
	int w = d == 1 ? p : 1;
	int h = -1;

	for(int i=0;i<4999;i++){
		for(int j=sj;j<sj+w;j++){
			if(t[i+1][j]){
				h = i;
				break;
			}
		}
		if(h != -1) break;
	}

	if(h == -1) h = 4999;

	if(d == 1){
		for(int j=sj;j<sj+p;j++) t[h][j] = 1;
	}
	else{
		for(int i=h;i>h-p;i--) t[i][sj] = 1;
	}
}

int countBlock(void){
	int res = 0;
	for(int i=4999;i>=0;i--){
		int count = 0;
		for(int j=0;j<5;j++){
			count += t[i][j];
			res += t[i][j];
		}
		if(count == 0) break;
	}
	return res;
}

int main(void){
	int n;

	while(cin>>n && n){
		memset(t,0,sizeof(t));
		for(int i=0;i<n;i++){
			int d,p,q;
			cin>>d>>p>>q;
			setBlock(d,p,q);
			destroy();
		}

		destroy();
		cout<<countBlock()<<endl;
	}

	return 0;
}