#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int x[1000][5], m[5], maxn, sum;
int n;
int a, b, c;
int main() {
	while (true) {
		sum = 0;
		memset(x, 0, sizeof(x));
		memset(m, 0, sizeof(m));
		cin >> n;
		if (n == 0) { break; }
		for (int h = 0; h < n; h++) {
			cin >> a >> b >> c;
			c--;
			if (a == 1) {
				maxn = 0;
				for (int j = c; j < c + b; j++) {
					maxn = max(maxn, m[j]);
				}
				for (int j = c; j < c + b; j++) {
					x[maxn][j] = 1;
					m[j] = maxn + 1;
				}
			}
			else {
				for (int j = 0; j < b; j++) {
					x[m[c] + j][c] = 1;
				}
				m[c] += b;
			}
			for (int i = 0; i < 1000; i++) {
				if (x[i][0] + x[i][1] + x[i][2] + x[i][3] + x[i][4] == 5) {
					x[i][0] = 0; x[i][1] = 0; x[i][2] = 0; x[i][3] = 0; x[i][4] = 0;
					for (int j = i; j < 999; j++) {
						for (int k = 0; k < 5; k++) {
							x[j][k] = x[j + 1][k];
						}
					}
					x[999][0] = 0; x[999][1] = 0; x[999][2] = 0; x[999][3] = 0; x[999][4] = 0;
					i--;
					m[0]--; m[1]--; m[2]--; m[3]--; m[4]--;
				}
			}
		}
		for (int i = 0; i < 1000; i++) {
			for (int j = 0; j < 5; j++) {
				sum += x[i][j];
			}
		}
		cout << sum << endl;
	}
}