#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>


int main(void)
{
	int result[1000], suffix = 0;

	while (1) {

		int nblock, direct, len, posit, tetris[1000][6] = { 0 };

		scanf("%d", &nblock);
		if (nblock == 0) {
			break;
		}

		for (int n = 0; n < nblock; n++) {
			scanf("%d %d %d", &direct, &len, &posit);

			int success;
			if (direct == 1) {
				for (int i = 1; i < 1000; i++) {
					success = 0;
					for (int j = posit; j < posit + len; j++) {
						if (tetris[i][j] == 1) {
							break;
						}

						for (int k = i; k <= i + 7; k++) {
							if (tetris[k][j] == 1) {
								success = 2;
								break;
							}
						}
						if (success == 2) {
							break;
						}

						if (j == posit + len - 1) {
							for (int k = 0; k < len; k++) {
								tetris[i][k + posit] = 1;
							}
							success = 1;
							break;
						}
					}
					if (success == 1) {
						break;
					}
				}
			}
			else {
				for (int i = 1; i < 1000; i++) {
					success = 0;
					for (int j = i; j < len + i; j++) {
						if (tetris[j][posit] == 1) {
							break;
						}

						for (int k = j; k <= 7; k++) {
							if (tetris[k][posit] == 1) {
								success = 2;
								break;
							}
						}
						if (success == 2) {
							break;
						}

						if (j == len + i - 1) {
							for (int k = 0; k < len; k++) {
								tetris[k + i][posit] = 1;
							}
							success = 1;
							break;
						}
					}
					if (success == 1) {
						break;
					}
					else if (success == 2) {
						continue;
					}
				}
			}
		}

		int sum = 0;
		for (int i = 1; i <= 1000; i++) {
			int a = 0, b = 0;
			for (int j = 1; j <= 5; j++) {
				if (tetris[i][j] == 0) {
					a++;
				}
				else {
					sum++;
					b++;
				}
			}

			if (a == 5) {
				break;
			}
			else if (b == 5) {
				sum -= 5;
			}
		}

		result[suffix++] = sum;
	}

	for (int i = 0; i < suffix; i++) {
		printf("%d\n", result[i]);
	}

	return 0;
}