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

#define REP(i,n,m) for(int i=n;i<m;i++)
#define rep(i,n) REP(i,0,n)

int t[5000][5];

void disp(void){
	for(int i=0;i<10;i++){
		for(int j=0;j<5;j++){
			cout<<(int)t[5000-10+i][j];
		}
		cout<<endl;
	}
	cout<<"---------------------\n";
}

bool stop(int ti,int tj,int d,int p){
	if(ti == 4999) return true;

	if(d == 1) REP(j,tj,tj+p){if(t[ti+1][j] == 1) return true;}
	else if(t[ti+1][tj]==1) return true;

	return false;
}

void putBlock(int ti,int tj,int d,int p){
	if(d == 1) REP(j,tj,tj+p) t[ti][j] = 1;
	else for(int i=ti;i>ti-p;i--) t[i][tj] = 1;
}

void dropBlock(int ti){
	for(int i=ti;i>0;i--){
		int sum = 0;
		rep(j,5){
			if(t[i-1][j] == 1) sum++;
			t[i][j] = t[i-1][j];
		}
		if(sum == 0) return;
	}
}

void deleteLine(int from,int to){
	int repeat = from - to;
	int i = from;
	rep(time,repeat){
		int sum = 0;
		rep(j,5) if(t[i][j] == 1) sum++;
		if(sum == 5){
			rep(j,5) t[i][j] = 0;
			dropBlock(i);
		}
		else{
			i--;
		}
	}
}

int main(void){
	int n;
	while(cin>>n,n){
		memset(t,0,sizeof(t));
		rep(i,n){
			int d,p,q;
			cin>>d>>p>>q;
			q--;
			int row;
			for(row=0;!stop(row,q,d,p);row++);
			putBlock(row,q,d,p);
			//disp();
			deleteLine(row,d==1?row-1:row-p);

			//disp();
		}

		int ans = 0;
		rep(i,5000) rep(j,5) if(t[i][j]==1) ans++;
		cout<<ans<<endl;
	}

	return 0;
}