#include <iostream>
#include <vector>
#include <cstring>

#define rep(i,n) for(int i=0;i<(n);i++)
#define YOKO 1
#define TATE 2

using namespace std;

struct B{
	int d;	// ubNÌü«@(1: ¡ü« 2: cü« )
	int p;	// ubNÌ·³  (1  d  5 )
	int q;	// ubNÌÊu@(1  p  5 ¡ü«ÌêÍ¶[ÌR}ª¿éÊu )
};

const int Hight = 5001;
const int Width = 5;

bool f[Hight][Width];

// cÁ½ubNðJEg
int count_block (void )
{
	int res = 0;

	for (int i = 0; i < Hight; i++ ){
		for (int j = 0; j < Width; j++ ){
			if (f[i][j] )
				res++;
		} // end for
	} // end for

	return res;
}

// ubNªlÜÁÄ¢éCðÁ·
void del_block (int n )
{
	for (int i = n + 1; i < Hight; i++ ){
		for (int j = 0; j < Width; j++ ){
			f[i-1][j] = f[i][j];
		} // end for
	} // end for
}

void is_all_row (void ) // ·×ÄubNªÜÁÄ¢éCðÁ·
{
	bool found = false;
	do{
		found = false;
		for (int i = 0; i < Hight; i++ ){
			int sum = 0;
			for (int j = 0; j < Width; j++ ){
				if (f[i][j] ){
					++sum;
				} // end if
			} // end for
			if (sum == Width ){
				del_block (i );
				found = true;
				break;			
			} // end if
		} // end for
	}while (found );
}

// row,colÌêÌãÌÊuªJ¢Ä¢é©Ç¤©Ì`FbN
bool check_tate (int row, int col )
{
	for (int i = row; i < Hight; i++ ){
		if (f[i][col] )
			return false;
	} // end for

	return true;
}

int main()
{
	int n;

	while (cin >> n && n ){
		memset (f, false, sizeof(f ) );
		vector <B> b (n );
		for (int i = 0; i < n; i++ ){
			cin >> b[i].d >> b[i].p >> b[i].q;
			b[i].q--;
		}// end for
	
		for (int i = 0; i < n; i++ ){
			int curr = 0;
			bool found = false; // »ÌÊuÉæqª é©Ç¤©
			do{
				found = false;	
				for (int j = b[i].q; j < b[i].q + b[i].p; j++ ){
					if (!check_tate (curr, j ) ){
						found = true;
						break;
					} // end if
					if (!f[curr][j] ){
						if (b[i].d == TATE ){	// cü«ÌêAê¾¯ÅOK
							break;
						}else{ 
							continue;
						} // end if
					}else{
						found = true;
						break;
					} // end if
				} // end for
				if (!found ){
					if (b[i].d == YOKO ){	// ¡ü«Ìê
						for (int j = b[i].q; j < b[i].q + b[i].p; j++ ){
							f[curr][j] = true;
						} // end for
					}else{					// cü«Ìê
						for (int j = 0; j < b[i].p; j++ ){
							f[curr+j][b[i].q] = true;
						} // end for 						
					} // end if
				}else{
					curr++;	// ²¸·ésðâ·
				} // end if
			}while (found );
			is_all_row ();	// ·×ÄÌCªlÜÁÄ¢é©`FbN
		} // end for

		int res = count_block();
		cout << res << endl;
	} // end loop
		
	return 0;
}