#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,};
    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;
	}
	row[maxh]+=p;
      }else{
	for(int j = h[q]; j < p + h[q]; ++j){
	  row[ j ]++;
	}
	h[q]+=p;
      }

      // cout << " DROPPED : " << i << endl;
      for(int j = 0; j < 5; ++j){
	mh = max( mh , h[j] );
      }

      for(int j = 0; j < mh; ++j){
	// cout << " row["<<j<<"] = " << row[j] << endl;
	if( row[j] == 5 ){
	  for(int k = 0; k < 5; ++k){
	    --h[k];
	  }
	  for(int k = j; k < mh; ++k){
	    row[k] = row[k + 1];
	  }
	  j = -1;
	  continue;
	}
      }
    }
    for(int i = 0; i < N; ++i){
      ans += row[i];
    }
    printf("%d\n",ans);
  }
  return 0;
}