#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define HORIZONTAL 1
#define VERTICAL 2
#define ALL 1+2+4+8+16
int main()
{
  int n,d,p,q;
  int num,block;
  while(cin>>n,n){
    vector<int>field;
    vector<int>::reverse_iterator it;
    queue<int>next;

    while(n-->0){
      cin>>d>>p>>q;
      if(d==HORIZONTAL){
	block=0;
	while(p-->0)block ^= 1<<(q+p-1);
	next.push(block);
      }else{
	block=1<<(q-1);
	while(p-->0)next.push(block);
      }
    }

    block=ALL;
    field.push_back(block);
    while(next.size()){
      block=next.front();
      next.pop();

      if(block==ALL)continue;

      if(field.back()&block){
	field.push_back(block);
	continue;
      }

      for(it=field.rbegin();it!=field.rend();it++){
	if(*it&block){
	  it--;
	  *it^=block;
	  if(*it==ALL){
	    it++;
	    field.erase(it.base());
	  }
	  break;
	}
      }
    }


    num=-5;
    for(it=field.rbegin();it!=field.rend();it++){
      for(int i=1;i<=16;i=i<<1){
	if(*it&i)num++;
      }
    }
    cout<<num<<endl;
  }
}