#include <iostream>
#include <algorithm>

using namespace std;

const int INF = 100000000;

int n;
bool table[5000][5];
int height[5];
int width[5000];
int ans;
int d, p, q;

void clear(){
  bool f = false;
  int h = -1;
  for(int i=0;i<5;i++){
    h = max(h, height[i]);
  }
  int pos = 0;
  for(int i=0;i<h;i++){
    if(width[i] == 5){
      width[i] = 0;
      ans -= 5;
      f = true;
      for(int j=0;j<5;j++){
	table[i][j] = false;
      }
    }else{
      if(i != pos){
	for(int j=0;j<5;j++){
	  table[pos][j] = table[i][j];
	  table[i][j] = false;
	}
	width[pos] = width[i];
	width[i] = 0;
      }
      ++pos;
    }
  }
  if(!f) return;
  fill(height, height+5, 0);
  for(int j=0;j<5;j++){
    for(int i=0;i<5000;i++){
      if(!table[i][j]) continue;
      height[j] = max(height[j], i+1);
    }
  }
}

main(){
  while(cin >> n && n){
    fill(table[0], table[5000], false);
    fill(height, height+5, 0);
    fill(width, width+5000, 0);
    ans = 0;
    for(int i=0;i<n;i++){
      cin >> d >> p >> q;
      --q;
      ans += p;
      if(d == 1){
	int h = -1;
	for(int j=0;j<p;j++){
	  h = max(h, height[q+j]);
	}
	for(int j=0;j<p;j++){
	  table[h][q+j] = true;
	  height[q+j] = h+1;
	}
	width[h] += p;
      }else if(d == 2){
	for(int j=0;j<p;j++){
	  table[height[q]+j][q] = true;
	  ++width[height[q]+j];
	}
	height[q] += p;
      }
      clear();
      /*for(int j=0;j<5;j++){
	cout << height[j] << ' ';
      }
      cout << endl;
      for(int j=0;j<8;j++){
	cout << width[j] << ' ';
      }
      cout << endl << endl;
      for(int i=9;i>=0;i--){
	for(int j=0;j<5;j++){
	  cout << table[i][j];
	}
	cout << endl;
      }
      cout << endl;*/
    }
    cout << ans << endl;
  }
  return 0;
}