#include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

int n, hmax;
bool maps[6000][5];

int serch(int x, int l) {
	for (int i = hmax; i >= 0; i--) {
		REP(j, l) if (maps[i][x + j]) return i + 1;
	}
	return 0;
}

int check(int y, int l) {
	REP(i, l) {
		bool flag = true;
		REP(j, 5) if (!maps[y + i][j]) flag = false;
		if (flag) return y + i;
	}
	return -1;
}

void bset(int d, int p, int q) {
	int hl = d == 2 ? p : 1;
	int xl = d == 1 ? p : 1;
	int hpos = serch(q - 1, xl);
	
	if (d == 1) REP(i, p) maps[hpos][q + i - 1] = true;
	else REP(i, p) maps[hpos + i][q - 1] = true;
	hmax = max(hmax, hpos + hl);
	
	int tmp;
	while ((tmp = check(hpos, hl)) != -1) {
		REP(i, 5) maps[tmp][i] = false;
		FOR(i, tmp, hmax) {
			REP(j, 5) swap(maps[i][j], maps[i + 1][j]);
		}
		hmax--;
	}
}

int main() {
	while (cin >> n, n) {
		memset(maps, 0, sizeof(maps));
		hmax = 0;
		
		while (n--) {
			int d, p, q;
			scanf("%d %d %d", &d, &p, &q);
			bset(d, p, q);
		}
		cout << count(maps[0], maps[6000], true) << endl;
	}
	return 0;
}