#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
using namespace std;

int ad,muki,pos,leng,hight[6],map1[6][6000];

int main(){
  while(true){
    for(int raw=1;raw<6;raw++){
      for(int high=0;high<6000;high++){
	map1[raw][high]=0;
      }
      hight[raw]=0;
    }
    int blo=0; 
    int x=0;
    cin>>ad;
    if(ad==0){break;}
    for(int i=0;i<ad;i++) {
      cin>>muki;
      cin>>leng;
      cin>>pos;
      blo+=leng;
      for(int raw=1;raw<6;raw++){
	for(int high=0;high<6000;high++){
	  if(map1[raw][high]==1){
	    hight[raw]=max(hight[raw],high);
	  }
	}
	if(map1[raw][hight[raw]]==1){
	  hight[raw]++;
	}
	///cout<<hight[raw]<<" ";
      }
      ///cout<<endl;
      if(muki==2){
	for(int hhhh=0;hhhh<leng;hhhh++){
	  map1[pos][hight[pos]+hhhh]=1;
	}
      }
      if(muki==1){
	int hamu=0;
	for(int raw=pos;raw<pos+leng;raw++){
	  hamu=max(hight[raw],hamu);
	}
	for(int raw=pos;raw<pos+leng;raw++){
	  map1[raw][hamu]=1;
	}
      }/*
      for(int high=0;high<10;high++){
	for(int raw=1;raw<6;raw++){
	  cout<<map1[raw][high]<<" ";
	}
	cout<<endl;
      }
      cout<<"-----------------------"<<endl;
       *///kesi
      for(int high=0;high<6000;high++){
	int hantei=0;
	for(int raw=1;raw<6;raw++){
	  if(map1[raw][high]==0){break;}
	  else{hantei++;}
	}
	if(hantei==5){
	  x++;
	  for(int j=1;j<6;j++){
	    hight[j]--;}
	  for(int a=1;a<6000-high;a++){
	    for(int raw=1;raw<6;raw++){
	      map1[raw][high+a-1]=map1[raw][high+a];
	    }
	  }
	  high--;
	}
      }
    }
    int xxx=0;
    xxx=blo-(5*x);
    cout<<xxx<<endl;
  }
}