#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;

			int width = d == 1 ? p : 1;
			int pos = -1;
			for (int i = 5000-1; pos == -1; --i)
				for (int j = 0; j < width; ++j)
					if (field[i][q + j] || i == -1)
					{
						pos = i + 1;
						break;
					}

			if (d == 1)
				for (int i = 0; i < p; ++i)
					field[pos][q + i] = true;
			else
				for (int i = 0; i < p; ++i)
					field[pos + i][q] = true;

			while (true)
			{
				bool vanished = false;
				for (int i = 0; i < 5000; ++i)
				{
					int j;
					for (j = 0; j < 5 && field[i][j]; ++j)
						;
					if (j == 5)
					{
						for (j = 0; j < 5; ++j)
							field[i][j] = false;
						vanished |= true;
					}
				}
				if (!vanished)
					break;

				int diff = 0;
				for (int i = 0; i < 5000; ++i)
				{
					int j;
					for (j = 0; j < 5 && !field[i][j]; ++j)
						;
					if (j == 5)
						++diff;
					else if (diff)
						for (int j = 0; j < 5; ++j)
						{
							field[i - diff][j] = field[i][j];
							field[i][j] = false;
						}
				}
			}
		}
		int ans = 0;
		for (int i = 0; i < 5000; ++i)
			for (int j = 0; j < 5; ++j)
				if (field[i][j])
					++ans;
		printf("%d\n", ans);
	}

	return 0;
}