#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int n, a, b, c;
int main() {
	while (cin >> n, n) {
		vector<vector<int> > d(5 * n + 1, vector<int>(5));
		vector<int> h(5);
		for (int i = 0; i < n; i++) {
			cin >> a >> b >> c; c--;
			if (a == 1) {
				int t = *max_element(h.begin() + c, h.begin() + c + b);
				fill(d[t].begin() + c, d[t].begin() + c + b, 1);
				fill(h.begin() + c, h.begin() + c + b, t + 1);
			}
			else {
				for (int i = h[c]; i < h[c] + b; i++) d[i][c] = 1;
				h[c] += b;
			}
			vector<int> w;
			for (int i = 5 * n; i >= 0; i--) {
				int sum = 0;
				for (int j = 0; j < 5; j++) sum += d[i][j];
				if (sum == 5) w.push_back(i);
			}
			for (int i = 0; i < w.size(); i++) {
				for (int j = 0; j < 5; j++) d[w[i]][j] = 0, h[j]--;
				for (int j = w[i]; j < 5 * n; j++) {
					for (int k = 0; k < 5; k++) d[j][k] = d[j + 1][k];
				}
			}
		}
		int ret = 0;
		for (int i = 0; i < 5 * n; i++) {
			for (int j = 0; j < 5; j++) ret += d[i][j];
		}
		cout << ret << endl;
	}
}