#include<iostream>
using namespace std;
int a[5000][5];
int main() {
	int n;
	while (cin >> n&&n != 0) {
		long long sum = 0;
		int h = 0;
		for (int i = 0; i < 5000; i++)
			fill(a[i], a[i] + 5, 0);
		for (int i = 0; i < n; i++) {
			int d, p, q;
			cin >> d >> p >> q;
			q--;
			if (d == 1) {
				int k = h;
				for (int j = h; j >= 0; j--) {
					bool b = true;
					for (int z = q; z < (p + q); z++) {
						if (a[j][z] == 1) b = false;
					}
					if (b)
						k = j;
					else
						break;
				}
				for (int z = q; z < (p + q); z++) {
					a[k][z] = 1;
				}
				if (k >= h) h = k+1;
				sum += p;
			}
			else {
				int k = h;
				for (int j = h; j >= 0; j--) {
					bool b = true;
					for (int z = 0; z < p; z++) {
						if (a[j + z][q] == 1) b = false;
					}
					if (b)
						k = j;
					else
						break;
				}
				for (int z = 0; z < p; z++) {
					a[k + z][q] = 1;
				}
				if (k + p > h) h = k + p;
				sum += p;
			}
			for (int i = h; i >= 0; i--) {
				bool b = true;
				for (int j = 0; j < 5; j++) {
					if (a[i][j] == 0) b = false;
				}
				if (b) {
					for (int j = i; j <= h+1; j++) {
						for (int z = 0; z < 5; z++) {
							a[i][z] = a[i + 1][z];
						}
					}
					h--;
					sum -= 5;
				}
			}
		}
		for (int i = 0; i < h; i++) {
			if (a[i][0] == 1 && a[i][1] == 1 && a[i][2] == 1 && a[i][3] == 1 && a[i][4] == 1)
				sum -= 5;
		}
		cout << sum << endl;
	}
}