#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <utility>
#include <tuple>
#include <math.h>
#include <iomanip>

using namespace std;

class Sover {
};

int main()
{
	while (true) {
		int n;
		cin >> n;
		if (n == 0)break;

		int length = n * 5 + 10;
		vector<vector<bool>> grid(length, vector<bool>(5, false));

		for (int i = 0; i < n; i++) {
			int d, p, q;
			cin >> d >> p >> q;
			q--;
			int bottom = length - 1;
			if (d == 1) {
				for (; bottom >= 0; bottom--) {
					bool interrupt = false;
					for (int j = 0; j < p; j++) {
						if (grid[bottom][q+j])interrupt = true;
					}
					if (interrupt)break;
				}

				for (int j = 0; j < p; j++)grid[bottom + 1][q + j] = true;
			}
			else {
				while (bottom >= 0 && !grid[bottom][q])bottom--;

				for (int j = 0; j < p; j++)grid[bottom + 1 + j][q] = true;
			}

			int shift_cnt = 0;
			for (int j = bottom + 1; j < length; j++) {
				bool full = true;
				for (int k = 0; k < 5; k++) {
					if (!grid[j][k]) {
						full = false;
						break;
					}
				}
				if (full) {
					shift_cnt++;
					for (int k = 0; k < 5; k++) {
						grid[j][k] = false;
					}
				}
				else {
					for (int k = 0; k < 5; k++) {
						grid[j - shift_cnt][k] = grid[j][k];
						if (shift_cnt > 0)grid[j][k] = false;
					}
				}
			}
		}

		int res = 0;
		for (int i = 0; i < length; i++) {
			for (int j = 0; j < 5; j++)if (grid[i][j])res++;
		}
		cout << res << endl;
	}
}
