#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

bool field[5000][5];
int top[5];

int main()
{
	int n;
	while (scanf("%d", &n), n)
	{
		memset(field, 0, sizeof(field));
		memset(top, 0, sizeof(top));

		while (n--)
		{
			int d, p, q;
			scanf("%d %d %d", &d, &p, &q);
			--q;
			if (d == 1)
			{
				int maxTop = -1;
				for (int i = 0; i < p; ++i)
					maxTop = max(maxTop, top[q + i]);
				for (int i = 0; i < p; ++i)
					field[maxTop][q + i] = true;
				for (int i = 0; i < p; ++i)
					top[q + i] = maxTop + 1;
			}
			else
			{
				for (int i = 0; i < p; ++i)
					field[top[q] + i][q] = true;
				top[q] += p;
			}
		}
		int sum = 0;
		for (int i = 0; i < 5000; ++i)
		{
			int s = 0;
			for (int j = 0; j < 5; ++j)
				if (field[i][j])
					++s;
			if (s != 5)
				sum += s;
		}
		printf("%d\n", sum);
	}

	return 0;
}