#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<cassert>
#include<sstream>
#include<numeric>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

#define MAX 100100

int b[25000][5];

int main(){
  int n;
  while(scanf("%d",&n), n){
    int h[5] = {0};
    REP(cc,n){
      int d,p,q;
      scanf("%d%d%d",&d,&p,&q);
      //printf("%d: d=%d, p=%d, q=%d\n",cc,d,p,q);
      --q;
      if(d == 1){
	int pos = *max_element(h+q, h+q+p);
	REP(i,p){
	  b[pos][q+i] = 1;
	  h[i] = pos+1;
	}
	if(accumulate(b[pos], b[pos]+5, 0) == 5){
	  REP(i,5) b[pos][i] = 0;
	}
      }else{
	int pos = h[q];
	REP(i,p) b[pos + i][q] = 1;
	h[q] += p;
	REP(i,p) if(accumulate(b[pos+i], b[pos+i]+5, 0) == 5)
	  REP(j,5) b[pos+i][j] = 0;
	int mx =*max_element(h, h+5);
	//printf("pos = %d\n",pos);
	for(int i=pos; i<mx-1; i++) if(accumulate(b[i], b[i]+5, 0) == 0){
	    int t = 0;
	    while(i+t < mx && accumulate(b[i+t], b[i+t]+5, 0) == 0)
	      ++t;
	    for(int j=i; j<mx; j++)
	      REP(k,5){
		b[j][k] = b[j+t][k];
		b[j+t][k] = 0;
	      }
	    --mx;
	    REP(k,5) --h[k];
	  }
      }
      REP(i,5){
	while(h[i] > 0 && b[h[i]-1][i] == 0)
	  h[i]--;
      }
      /*
      int mx = *max_element(h, h+5);
      for(int i=mx-1; i>=0; i--) { REP(j,5) printf("%d ",b[i][j]); puts(""); }
      printf("----------\n");
      REP(i,5) printf("%d ",h[i]); puts("");puts("");
      */
    }
    int mx = *max_element(h, h+5);
    int ans = 0;
    REP(i,mx) REP(j,5) if(b[i][j] == 1){
      b[i][j] = 0;
      ++ans;
    }
    printf("%d\n",ans);
  }
  return 0;
}