#include<iostream>
#include<algorithm>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

const int N = 6000;
//const int N = 10;
bool block[N][5];


void output(){
  cout <<"Begin"<<endl;
  rep(i,10){
    rep(j,5){
      cout << block[9-i][j] << " ";
    }
    cout << endl;
  }
  cout <<"end"<<endl;
}

void all_fall(int y){
  rep(j,5)block[y][j]=false;

  
  REP(i,y,N-1){
    rep(j,5)block[i][j]=block[i+1][j];

  }
  
  rep(j,5)block[N-1][j]=false;
}

int getmax(int l,int r){
  int ret=0;
  REP(j,l,r){    
    int tmp = N-1;
    for(int i=N-1;i>=0;i--){
      if (block[i][j])break;
      tmp=i;
    }
    ret=max(ret,tmp);
  }
  return ret;
}

void fall(){
  rep(i,N){
    if (block[i][0]&block[i][1]&block[i][2]&block[i][3]&block[i][4]){
      all_fall(i);
      i--;
    }
  }
}

int main(){
  int n;
  while(cin>>n && n){
    rep(i,N)rep(j,5)block[i][j]=false;
    rep(i,n){
      int d,p,q;
      int wid=1,hei=1,pos;
      cin>>d>>p>>q;
      q--;
      if (d == 1){//yoko
	pos = getmax(q,q+p);	
	wid=p;
      }else if (d == 2){//tate
	pos = getmax(q,q+1);
	hei=p;
      }

      rep(i,hei){
	rep(j,wid){
	  block[pos+i][q+j]=true;
	}
      }
      fall();
      //output();
    }

    cout << count(&block[0][0],&block[N][0],true) << endl;
    //    cout << cnt << endl;
  }
  
}