#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main() {
	int n;
	while(cin >> n) {
		if (n == 0) {
			break;
		}
	vector<int> retu[5];
	for (int i = 0; i < n; i++) {
		int d, p, q;
		cin >> d >> p >> q;
		q--;
		if (d == 1) {
			int MAX = 0;
			for (int j = q; j < q+p; j++) {
				int k = retu[j].size();
				MAX = max(MAX, k);
			}
			for (int j = q; j < q+p; j++) {
				for (int k = retu[j].size(); k < MAX; k++) {
					retu[j].push_back(0);
				}
				retu[j].push_back(1);
			}
		} else {
			for (int j = 0; j < p; j++) {
				retu[q].push_back(1);
			}
		}
		int MIN = retu[0].size();
		for (int j = 1; j < 5; j++) {
			int k = retu[j].size();
			MIN = min(MIN, k);
		}
		for (int j = MIN-1; j >= 0; j--) {
			if (retu[0][j]+retu[1][j]+retu[2][j]+retu[3][j]+retu[4][j] == 5) {
				for (int k = 0; k < 5; k++) {
					for (int l = j+1; l < retu[k].size(); l++) {
						retu[k][l-1] = retu[k][l];
					}
					vector<int>::iterator it = retu[k].end();
					it--;
					retu[k].erase(it);
				}
			}
		}
		for (int j = 0; j < 5; j++) {
			vector<int>::iterator it = retu[j].end();
			vector<int>::iterator itt = retu[j].begin();
			it--;
			for (int k = retu[j].size()-1; k >= 0; k--) {
				if (retu[j][k] == 0) {
					retu[j].erase(it);
				} else {
					break;
				}
				it--;
			}
		}
	}
	int counter = 0;
	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < retu[i].size(); j++) {
			if (retu[i][j] == 1) {
				counter++;
			}
		}
	}
	cout << counter << endl;
	}
	return 0;
}