#include <bits/stdc++.h>
using namespace std;
const double eps=1e-9;
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;
int N;
int board[6005][6];//たて:1~5000,よこ:1~5;
int mxcol[6];

int setblock(int d,int p,int q)
{
  int setheight=0;
  if(d==1)
    {
      for(int i=q;i<p+q;i++)
	{
	  setheight=max(setheight,mxcol[i]);
	}
      //cout << setheight << endl;
      for(int row=q;row<p+q;row++)
	{
	  board[setheight+1][row]=1;
	}
      for(int i=q;i<p+q;i++) mxcol[i]=setheight+1;
    }
  else if(d==2)
    {
      setheight=mxcol[q];
      for(int col=setheight;col<setheight+p;col++)
	{
	  board[col+1][q]=1;
	}
      mxcol[q]=setheight+p;
    }
  return setheight+1;
}

void vanishcheck(int minheight,int p,vector<int> &vanishcol)
{
  bool vanish;
  for(int col=minheight;col<minheight+p;col++)
    {
      vanish=true;
      for(int row=1;row<=5;row++)
	{
	  if(board[col][row]==0) vanish=false;
	}
      if(vanish)
	{
	  for(int row=1;row<=5;row++) board[col][row]=0;
	  vanishcol.push_back(col);
	}
    }
  return;
}

void aftervanish(vector<int> vanishcol)
{
  int mxrow=0;
  for(int i=1;i<=5;i++)
    {
      mxrow=max(mxrow,mxcol[i]);
    }
  for(int i=0;i<int(vanishcol.size());i++)
    {
      for(int j=1;j<=5;j++) mxcol[j]--;
      for(int row=vanishcol[i]-i;row<=mxrow;row++)
	{
	  for(int col=1;col<=5;col++)
	    {
	      swap(board[row][col],board[row+1][col]);
	    }
	}
    }
  for(int row=1;row<=5;row++)
    {
      for(int col=mxcol[row];col>=0;col--) 
	{
	  if(board[col][row]==1 || col==0)
	    {
	      mxcol[row]=col;
	      break;
	    }
	}
    }
}

int main(void)
{
  int minheight;
  while(cin >> N && N)
    {
      int ans=0;
      memset(board,0,sizeof(board));
      memset(mxcol,0,sizeof(mxcol));
      for(int i=0;i<N;i++)
	{
	  int d,p,q;
	  cin >> d >> p >> q;
	  minheight=setblock(d,p,q);
	  vector<int> vanishcol;
	  if(d==1)
	    {
	      vanishcheck(minheight,1,vanishcol);
	    }
	  else if(d==2)
	    {
	      vanishcheck(minheight,p,vanishcol);
	    }
	  if(int(vanishcol.size()!=0))
	    {
	      sort(vanishcol.begin(),vanishcol.end());
	      aftervanish(vanishcol);
	    }
	  /*
	  cout << "NOWcol" << endl;
	  for(int row=1;row<=5;row++)
	    {
	      cout << mxcol[row] << ' ';
	    }
	  cout << endl;
	  for(int col=10;col>=1;col--)
	    {
	      for(int row=1;row<=5;row++)
		{
		  cout << board[col][row];
		}
	      cout << endl;
	      }*/
	}
      for(int i=1;i<=6000;i++)
	{
	  for(int j=1;j<=5;j++)
	    {
	      if(board[i][j]==1) ans++;
	    }
	}
      cout << ans << endl;
    }
  return 0;
}

