#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int main()
{
	int n;
	while(cin >> n && n)
	{
		vector<vector<int> > a;
		rep(i,5001)
		{
			vector<int> temp(5);
			rep(j,5) temp[j] = 0;

			a.push_back(temp);
		}

		int d,p,q,ans = 0;
		
		rep(i,n)
		{
			cin >> d >> p >> q;
			q--;
			if(d == 1)
			{
				int ny = 5000;
				
				while(true)
				{
					bool flag = true;
					REP(j,q,q+p)
					{
						if(a[ny][j] != 0) flag = false;
					}

					if(flag) 
					{
						if(ny == 0) break;
						else
						{
							bool flag_2 = true;
							REP(j,q,q+p)
							{
								if(a[ny-1][j] != 0) flag_2 = false;
							}

							if(!flag_2) break;
						}
					}

					ny--;
				}

				REP(j,q,q+p)
				{
					a[ny][j] = 1;
				}
			}
			else if(d == 2)
			{
				int ny = 5000;

				while(true)
				{
					bool flag = true;

					for(int j=ny;j>ny-p;j--)
					{
						if(a[j][q] != 0) flag = false;
					}

					if(flag) 
					{
						if(ny-p+1 == 0) break;
						else
						{	
							bool flag_2 = true;

							for(int j=ny-1;j>ny-1-p;j--)
							{
								if(a[j][q] != 0) flag_2 = false;
							}

							if(!flag_2) break;
						}
					}
					
					ny--;
				}

				for(int j=ny;j>ny-p;j--)
				{
					a[j][q] = 1;
				}
			}

			rep(j,a.size())
			{
				bool flag = true;

				rep(k,5)
				{
					if(a[j][k] != 1) flag = false;
				}

				if(flag) 
				{
					a.erase(a.begin() + j);
					j--;
				}
			}
			
		}

		rep(j,5001) rep(k,5) if(a[j][k] == 1) ans++;
		
		cout << ans << endl;
	}
	return 0;
}