#include <iostream>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
int main(){
	int n, d, p, q;
	while(cin >> n, n) {
		bool b[5010][5] = {};
		int up = 0;
		rep(i,n) {
			cin >> d >> p >> q;
			--q;
			bool f = 1;
			if(d == 1) {
				for(int j=up-1;j>=0;--j) {
					rep2(i,q,p+q) {				
						if(b[j][i]) {
							rep2(k,q,p+q) {
								b[j+1][k] = 1;
							}
							up = j == up-1 ? up+1 : up;
							f = 0;
							break;
						}
					}
					if(!f) break;
				}
				if(f) {
					rep2(i,q,p+q) {
						b[0][i] = 1;
					}
					up = up == 0 ? 1 : up;
				}
			} else {
				for(int j=up-1;j>=0;--j) {
					if(b[j][q]) {
						rep2(i,1,p+1) {
							b[j+i][q] = 1;
						}
						up = up <= j+p+1 ? j+p+1 : up;
						f = 0;
						break;
					}
				}
				if(f) {
					rep(i,p) {
						b[i][q] = 1;
					}
					up = up < p ? p : up;
				}
			}
			int cnt = 0;
			rep(i,up) {
				bool c = 1;
				rep(j,5) {
					if(!b[i][j]) {
						c = 0;
						break;
					}
				}
				if(c) {
					rep2(j,i,up) {
						rep(k,5) {
							b[j][k] = b[j+1][k];
						}
					}
					i--;
					cnt++;
				}
			}
			up -= cnt;
		}
		int ret = 0;
		rep(i,up) {
			rep(j,5) {
				if(b[i][j]) ret++;
			}
		}
		cout << ret << endl;
	}
	return 0;
}