#include <iostream>
#include <algorithm>

using namespace std;

void solve()
{
	bool isUsed[5002][5];
	int n;
	while (cin >> n, n)
	{
		for (int i = 1; i < 5002; ++i)
		{
			for (int j = 0; j < 5; ++j)
			{
				isUsed[i][j] = false;
			}
		}
		for (int i = 0; i < 5; ++i)
		{
			isUsed[0][i] = true;
		}
		int highest = 1;
		for (int i = 0; i < n; ++i)
		{
			int d, p, q;
			cin >> d >> p >> q;
			--q;
			int level = 0;
			if (d == 1)
			{
				bool isOK = true;
				while (isOK)
				{
					for (int j = 0; j < p; ++j)
					{
						if (isUsed[highest - level][q + j])
						{
							isOK = false;
							--level;
							break;
						}
					}
					++level;
				}
				--level;
				for (int j = 0; j < p; ++j)
				{
					isUsed[highest - level][q + j] = true;
				}
				highest = max(highest, highest - level + 2);
			}
			else if (d == 2)
			{
				while (!isUsed[highest - level][q])
				{
					++level;
				}
				--level;
				for (int j = 0; j < p; ++j)
				{
					isUsed[highest - level + j][q] = true;
				}
				highest = max(highest, highest - level + p + 1);
			}
		}
		int ans = 0;
		for (int i = 1; i < highest; ++i)
		{
			int rowCount = 0;
			for (int j = 0; j < 5; ++j)
			{
				if (isUsed[i][j])
				{
					++ans;
					++rowCount;
				}
				if (rowCount == 5)
				{
					ans -= 5;
				}
			}
		}
		cout << ans << endl;
	}
}

int main()
{
	solve();
	return(0);
}