#include<iostream>
#define N 10020
using namespace std;
int main(){
  while(true){
    int n;
    int ans=0;
    scanf("%d",&n);

    if(n==0)break;
    int row[N]={0,};
    int h[5]={0,};
    short M[5][N]={{0,},};
    
    for(int i = 0; i < n; ++i){
      int mh = 0;
      int d,p,q;
      scanf("%d%d%d", &d, &p, &q);
      --q;
      
      if(d==1){
	int maxh = 0;
	for(int j = q; j < q + p; ++j){
	  maxh = max( maxh, h[j] );
	}
	for(int j = q; j < q + p; ++j){
	  h[j]=maxh+1;
	  M[j][maxh]=1;
	}
	row[maxh]+=p;
      }else{
	for(int j = h[q]; j < p + h[q]; ++j){
	  ++row[ j ];
	  M[q][j]=1;
	}
	h[q]+=p;
      }

      for(int j = 0; j < 5; ++j){
	mh = max( mh , h[j] );
      }
      /*
      cout << " STACKED : " << endl;
      for(int j = mh; j >= 0; --j){
	for(int k = 0; k < 5; ++k){
	  cout << M[k][j];
	}
	cout << endl;
      }
      cout << endl;
      */
      for(int j = 0; j < mh+1; ++j){
	if( row[j] == 5 ){
	  for(int k = j; k < mh+2; ++k){
	    row[k] = row[k+1];
	    for(int l = 0; l < 5; ++l){
	      M[l][k] = M[l][k+1];
	    }
	  }
	  j = -1;
	}
      }
      /*
      cout << " ERASED : " << endl;
      for(int j = mh; j >= 0; --j){
	for(int k = 0; k < 5; ++k){
	  cout << M[k][j];
	}
	cout << endl;
      }
      cout << endl;
      */
      for(int j = 0; j < 5; ++j){
	h[j] = 0;
      }
      for(int j = 0; j < 5; ++j){
	for(int k = 0; k < mh+3; ++k){
	  if( M[j][k] == 1 ){
	    h[j] = k+1;
	  }
	}
      }
      /*
      cout << " HEIGHTS : " << endl;
      for(int j = 0; j < 5; ++j){
	cout << ' ' << h[j] << endl;
      }
      */
    }
    for(int i = 0; i < 5; ++i){
      for(int j = 0; j < N; ++j){
	if( M[i][j] )
	  ans++;
      }
    }
    printf("%d\n",ans);
  }
  return 0;
}