#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#ifdef _WIN32
#define __builtin_popcount __popcnt
#endif
int n, d, p, q;
int main() {
	while (cin >> n, n) {
		vector<int> bit;
		vector<int> c(5);
		while (n--) {
			cin >> d >> p >> q; q--;
			if (d == 1) {
				int r = *max_element(c.begin() + q, c.begin() + q + p);
				int b = (1 << (q + p)) - (1 << q);
				if (bit.size() == r) bit.push_back(b);
				else bit[r] |= b;
			}
			else {
				for (int i = c[q]; i < c[q] + p; i++) {
					if (bit.size() == i) bit.push_back(1 << q);
					else bit[i] |= 1 << q;
				}
			}
			vector<int> bit2;
			for (int i : bit) {
				if (i != 31) bit2.push_back(i);
			}
			bit = bit2;
			for (int i = 0; i < 5; i++) {
				c[i] = 0;
				for (int j = bit2.size() - 1; j >= 0; j--) {
					if (bit2[j] & (1 << i)) {
						c[i] = j + 1; break;
					}
				}
			}
		}
		int ret = 0;
		for (int i : bit) ret += __builtin_popcount(i);
		cout << ret << endl;
	}
	return 0;
}