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

#define for_(i,a,b) for(int i=(a);i<(b);++i)

int n;
bool block[5050][5];

void down(int py) {
	for_(y,py,5010) {
		for_(x,0,5) {
			block[y][x] = block[y + 1][x];
			block[y + 1][x] = false;
		}
	}
}

void vanish() {
	for_(y,0,5050) {
		bool van = true;
		
		for_(x,0,5) van &= block[y][x];
		
		if (van) {
			down(y);
			--y;
		}
	}
}

void fall(int dir, int len, int lft) {
	int by = 5010, bx = lft;
	
	while (by > 0) {
		bool end = false;
		
		if (dir) {
			end |= block[by - 1][bx];
		} else {
			for_(x,bx,bx+len) end |= block[by - 1][x];
		}
		
		if (end) break;
		
		--by;
	}
	
	if (dir) {
		for_(y,by,by+len) block[y][bx] = true;
	} else {
		for_(x,bx,bx+len) block[by][x] = true;
	}
}

void play() {
	for_(i,0,n) {
		int a, b, c;
		cin >> a >> b >> c;
		fall(--a, b, --c);
		vanish();
	}
	
	int ans = 0;
	for_(y,0,5050) for_(x,0,5) ans += block[y][x];
	cout << ans << endl;
}

int main() {
	while (cin >> n, n) {
		memset(block, false, sizeof(block));
		play();
	}
}