#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

int MapData[5001][6];
int N;


void solveHori(int len, int pos) {
	bool findFirstSquare;

	for (int row = 5000; row > -1; --row) {
		findFirstSquare = false;
		for (int j = 0; j < len; ++j) {
			if (MapData[row][pos+j] == 1) {
				findFirstSquare = true;
				break;
			}
		}
		if (!findFirstSquare) {
			continue;
		}
		for (int j = 0; j < len; ++j) {
			MapData[row+1][pos + j] = 1;
		}
		return;
	}
	for (int j = 0; j < len; ++j) {
		MapData[0][pos + j] = 1;
	}
}

void solveVert(int len, int pos) {
	bool findFirstSquare;

	for (int row = 5000; row > -1; --row) {
		findFirstSquare = false;
		if (MapData[row][pos] == 1) {
			findFirstSquare = true;
		}
		if (!findFirstSquare) {
			continue;
		}
		for (int j = 0; j < len; ++j) {
			MapData[row + 1 + j][pos] = 1;
		}
		return;
	}
	for (int j = 0; j < len; ++j) {
		MapData[j][pos] = 1;
	}
}

void modifyMapData(int row) {

	for (int i = row; i < 5000; i++) {
		for (int j = 1; j < 6; j++) {
			MapData[i][j] = MapData[i + 1][j];
		}
	}
	
}

void earseMapdata() {
	int cntZero, cntOne;
	
	for (int row = 0; row < 5001; ++row) {
		cntZero = cntOne = 0;
		for (int col = 1; col < 6; ++col) {
			if (MapData[row][col] == 1) {
				++cntOne;
			}
			else {
				++cntZero;
			}
		}
		if (cntZero == 5) {
			return;
		}
		if (cntOne == 5) {
			modifyMapData(row);
			--row;
		}
	}
}

int main() {
	int d, p, q, cnt;

	while (cin >> N, N) {
		memset(MapData, 0, sizeof(MapData));
		for (int i = 0; i < N; ++i) {
			cin >> d >> p >> q;

			if (d == 1) {
				//横向き
				solveHori(p, q);
			}
			else if (d == 2) {
				//縦向き
				solveVert(p, q);
			}
			earseMapdata();
		}
		cnt = 0;
		for (int row = 0; row < 5000; ++row) {
			for (int col = 1; col < 6; ++col) {
				if (MapData[row][col] == 1) {
					++cnt;
				}
			}
		}
		cout<<cnt<<endl;
	}

	return 0;
}


