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

int main()
{
	int n;
	while (scanf("%d", &n), n){
		vector<int> board(1, (1 << 5) - 1);
		int res = 0;
		for (int i = 0; i < n; i++){
			int d, p, q;
			scanf("%d %d %d", &d, &p, &q);
			--q;
			
			int block = 0, num;
			if (d == 1){
				for (int j = 0; j < p; j++){
					block |= 1 << (q + j);
				}
				num = 1;
			}
			else {
				block = 1 << q;
				num = p;
			}
			
			for (int j = 0; j < num; j++){
				for (int h = board.size(); h > 0; h--){
					if (!(board[h - 1] & block)) continue;
					if (h == board.size()) board.push_back(0);
					board[h] |= block;
					break;
				}
			}
			
			res += p;
			for (int h = board.size() - 1; h > 0; h--){
				if (board[h] == (1 << 5) - 1){
					board.erase(board.begin() + h);
					res -= 5;
				}
			}
		}
		printf("%d\n", res);
	}
	return 0;
}