#include <iostream>
#include <vector>
using namespace std;
int mp[10000000][5];
//d<muki p<nagasa q<basyo
int d,p,q;
int ktflg = 0,th;

void deb() {
  for(int i=0;i<10;i++) {
    for(int j=0;j<5;j++)
      cout << mp[i][j] << " ";
    cout << endl;
  }
  cout << "----------------------------" <<endl;

}


void tumeru() { 
  
  int t;
  for(int i=1;i<th;i++) {
    for(int j=0;j<5;j++) {
      if(mp[i][j] > 0 && ((j>0 && mp[i][j] != mp[i][j-1]) || j==0)){
	//	cout <<j<<" "<<mp[i][j]<< endl;
	int o=i;
	t = mp[i][j];
	while(1) {
	int count = 0;
	for(int k=j;k<5;k++) if(t == mp[o][k]) count++;
	int flg = 0;
	for(int k=j;k<count;k++) {
	  if(mp[o-1][k] != 0) break;
	  if(k == count-1) flg = 1;
	}
	if(flg == 1){
	  for(int k=j;k<count;k++) mp[o-1][k] = t , mp[o][k] = 0;
	  o--;
	  ktflg = 1;
	}
	if(flg == 0 || o == 0) break;
	  }
      }
      if(mp[i][j] < 0 && mp[i-1][j] == 0) {
	int t = i;
	ktflg = 1;
	while(1){
	  if(t-1 < 0 || mp[t-1][j] !=0) break;
	  mp[t-1][j] = mp[t][j],mp[t][j] = 0;
	  t--;
	}
      }
	
    }
  }
}

void kesu() {

     int h = 0;
  while(1) {
    int flg =0;
    for(int i=0;i<5;i++) {
      if(mp[h][i] !=0) break;
      if(i == 4) flg = 1;
    }
    if(flg == 1) break;
    h++;
  }
  th = h;

  h = 0;
  while(1) {
    int zero = 0;
    int count = 0;
    for(int i=0;i<5;i++) {
      if(mp[h][i] == 0) zero++;
      else count++;
    }

    if(zero == 5) break;
    if(count == 5)
      for(int i=0;i<5;i++)
	mp[h][i] = 0;
    h++;
  }

  
}
       

void add(int c) {

  int h = 0;
  while(1) {
    int flg =0;
    for(int i=0;i<5;i++) {
      if(mp[h][i] !=0) break;
      if(i == 4) flg = 1;
    }
    if(flg == 1) break;
    h++;
  }
  
  if(d == 1) {
    while(1) {
      if(h <= 0)break;
      int flg = 0;
      for(int i=0;i<p;i++)
	if(mp[h-1][i+q-1] != 0) break;
	else if(i == p-1) h-- ,flg = 1;
      if(flg == 0) break;
    }
  
  
    for(int i=0;i<p;i++)
      mp[h][i+q-1] = c;
  }
  
  if(d == 2) {
    while(1) {
      int flg = 0;
    for(int i=0;i<p;i++){
      if(h-i <0 || mp[h-i][q-1] != 0) break;
      if(i == p-1)flg =1;
    }
    if(flg == 1){
      if(h-p >= 0 && mp[h-p][q-1] == 0){
	h--;
	continue;
      }
      for(int i=0;i<p;i++)mp[h-i][q-1] = -1*c;
      break;
    }
    h++;
    }
    
  }
  
}

int main() {

  int ques;
  while(1) {
  cin >> ques;
  if(ques == 0) break;
  for(int i=0;i<50000;i++)
    for(int j=0;j<5;j++)
      mp[i][j] = 0;
  
  int c = 1;
  for(int i=0;i<ques;i++) {
    cin >> d >> p >>q;
    //cout << d <<" " << p <<" "<<q<<endl;
    add(c);
    c++;
  }
  //deb();
  ktflg = 1;
  while(1) {
    if(ktflg == 0) break;
    ktflg = 0;
    kesu();
    tumeru();
  }
  //    deb();
    
  int h=0;
  int ans = 0;
  while(1) {
    int zero = 0;
    for(int i=0;i<5;i++){
      if(mp[h][i] == 0) zero++;
      else ans++;
    }
    if(zero == 5) break;
    h++;
  }
  cout << ans << endl;

  }
    return 0;
  
}