#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

int map[10000][10];

void del(int y)
{
	for (; y < 9999; y++){
		for (int i = 0; i < 5; i++){
			map[y][i] = map[y + 1][i];
		}
	}
}

int main(void)
{
	int n;
	while (cin >> n, n){
		memset(map, 0, sizeof(map));
		
		for (int i = 1; i <= n; i++){
			int d, p, q;
			
			cin >> d;
			cin >> p;
			cin >> q;
			q--;
			
			if (d == 2){
				int y = 10000 - p;
				while (map[y - 1][q] == 0 && y > 0){
					y--;
				}
				
				for (int j = y; j < y + p; j++){
					map[j][q] = i;
				}
			}
			
			if (d == 1){
				int y = 10000 - p;
				while (1){
					int f = 0;
					
					for (int j = q; j < 5; j++){
						if (map[y - 1][j] != 0){
							f = 1;
							break;
						}
					}
					
					if (f == 1 || y == 0){
						break;
					}
					
					y--;
				}
				
				for (int j = q; j < q + p; j++){
					map[y][j] = i;
				}
			}
			
			int y = 0;
			while (y < 10000){
				int cnt = 0;
				
				for (int j = 0; j < 5; j++){
					cnt += !!map[y][j];
				}
				
				if (cnt == 5){
					del(y);
				}
				else {
					y++;
				}
			}
/*			
			for (int y = 0; y < 20; y++){
				for (int j = 0; j < 5; j++){
					printf("%d ", map[y][j]);
				}
				puts("");
			}
*/
		}
		
		int cnt = 0;
		for (int y = 0; y < 10000; y++){
			for (int j = 0; j < 5; j++){
//				printf("%d ", map[y][j]);
				cnt += !!map[y][j];
			}
			
//			puts("");
		}
		
		printf("%d\n", cnt);
	}
	
	return (0);
}