#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

bool map[5000][6];
int N;

void func(){

	int max_height = 5*N-1;

	for(int row = 0; row <= max_height; row++){
		for(int col = 1; col <= 5; col++){
			map[row][col] = false;
		}
	}

	int top_pos[6] = {0,0,0,0,0,0};

	int dir,len,loc,tmp_max;
	stack<int> delete_row;
	bool FLG;

	for(int loop = 0; loop < N; loop++){
		scanf("%d %d %d",&dir,&len,&loc);

		if(dir == 1){
			tmp_max = 0;
			for(int col = loc; col <= loc+len-1; col++){
				tmp_max = max(tmp_max,top_pos[col]);
			}

			for(int col = loc; col <= loc+len-1; col++){
				map[tmp_max][col] = true;
				top_pos[col] = tmp_max+1;
			}
		}else{
			for(int i = 0; i < len; i++){
				map[top_pos[loc]+i][loc] = true;
			}
			top_pos[loc] += len;
		}

		tmp_max = 0;
		for(int col = 1; col <= 5; col++){
			tmp_max = max(tmp_max,top_pos[col]);
		}

		for(int row = 0; row <= tmp_max;row++){
			FLG = true;
			for(int col = 1; col <= 5; col++){
				if(map[row][col] == false){
					FLG = false;
					break;
				}

			}
			if(FLG){
				for(int col = 1; col <= 5; col++){
					map[row][col] = false;
				}
				delete_row.push(row);
			}
		}

		while(!delete_row.empty()){

			for(int col = 1; col <= 5; col++){
				for(int row = delete_row.top()+1; row <= top_pos[col]; row++){
					if(map[row][col]){
						map[row][col] = false;
						map[row-1][col] = true;
					}
				}
				for(int row = top_pos[col]; row >= 0; row--){
					if(row == 0){
						if(map[row][col]){
							top_pos[col] = 1;
						}else{
							top_pos[col] = 0;
						}
						break;
					}else{
						if(map[row][col]){
							top_pos[col] = row+1;
							break;
						}
					}
				}
			}

			tmp_max = 0;
			for(int col = 1; col <= 5; col++){
				tmp_max = max(tmp_max,top_pos[col]);
			}

			for(int row = 0; row <= tmp_max;row++){
				FLG = true;
				for(int col = 1; col <= 5; col++){
					if(map[row][col] == false){
						FLG = false;
						break;
					}

				}
				if(FLG){
					for(int col = 1; col <= 5; col++){
						map[row][col] = false;
					}
					delete_row.push(row);
				}
			}
			delete_row.pop();
		}
	}

	max_height = 0;
	for(int col = 1; col <= 5; col++){
		max_height = max(max_height,top_pos[col]);
	}

	int ans = 0;
	for(int row = 0; row <= max_height; row++){
		for(int col = 1; col <= 5; col++){
			if(map[row][col]){
				ans++;
			}
		}
	}

	printf("%d\n",ans);
}

int main(){

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		func();
	}
	return 0;
}