#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int main() {
	int n;
	while (scanf("%d", &n), n) {
		deque<int>E[5];
		rep(i, n) {
			int d, p, q; scanf("%d%d%d", &d, &p, &q); q--;
			if (d == 1) {
				int Max = 0;
				rep(i, p)Max = max(Max, (int)E[q + i].size());
				rep(i, p) {
					int k = E[q + i].size();
					rep(j, Max - k)E[q + i].push_back(0);
					E[q + i].push_back(1);
				}
			}
			else {
				rep(i, p)E[q].push_back(1);
			}
			int Min = INT_MAX;
			rep(i, 5)Min = min(Min, (int)E[i].size());
			rep(i, Min) {
				bool ok = true;
				rep(j, 5)ok &= E[j][i];
				if(ok)rep(j, 5)E[j][i] = -1;
			}
			rep(i, 5)E[i].erase(remove(E[i].begin(), E[i].end(), -1), E[i].end());
		}
		int sum = 0;
		rep(i, 5)rep(j, E[i].size()) {
			if (E[i][j])sum++;
		}
		printf("%d\n", sum);
	}
}