#include <iostream>
#include <vector>
using namespace std;
#define HORIZONTAL 1
#define VERTICAL 2

int bit(int bits)
{
	int ret = 0;
	while(bits != 0)
	{
		ret += bits & 0x00000001;
		bits >>= 1;
	}
	return ret;
}

int main()
{
	int n, a, b, c, block, ret;
	while(cin >>n, n)
	{
		vector<int> data(5001, 0);
		data[5000] = 63; /* 63 = 0x11111 */
		ret = 0;
		
		while(n--)
		{
			cin >> a >> b >> c;
			c--;
			if(a==HORIZONTAL)
			{
				block = ((1<<b)-1) << c;
				for(int i=0; i<data.size()-1; i++)
				{
					if(data[i+1] & block)
					{
						data[i] += block;
						break;
					}
				}
			}
			else
			{
				block = 1 << c;
				for(int i=0; i<data.size()-1; i++)
				{
					if(data[i+1] & block)
					{
						for(int j=0; j<b; j++)
						{
							data[i-j] += block;
						}
						break;
					}
				}
			}

			vector<int>::iterator it = data.begin();
			while(it != data.end())
			{
				if((*it) == 31)
				{
					it = data.erase(it);
					it--;
				}
				it++;
			}
		}
		ret = 0;
		for(int i=0; i<data.size()-1; i++)
		{
			ret += bit(data[i]);
		}
		cout << ret << endl;
	}
}