#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main(){
  int n,d,p,q;

  for(;;){
    cin >> n;
    if(!n)break;

    vector<string> board;
    for(int z=0;z<n;z++){
      cin >> d >> p >> q;

      int x,y;
      if(d==1){
	x = p;
	y = 1;
      }else{
	x = 1;
	y = p;
      }

      int tmp = -1;
      for(int i=0;i<x;i++){
	for(int j=(int)board.size()-1;j>=0;j--){
	  if(board[j][q+i-1]=='#'){
	    tmp = max(tmp,j);
	    break;
	  }
	}
      }
      for(int i=tmp+1;i<(int)board.size();i++){
	for(int j=0;j<x;j++)board[i][q+j-1] = '#';
	y--;
	if(!y)break;
      }

      string block;
      for(int i=0;i<q-1;i++)block.push_back('.');
      for(int i=0;i<x;i++)block.push_back('#');
      while(block.size()<5)block.push_back('.');
      while(y--)board.push_back(block);

      for(int i=0;i<(int)board.size();i++){
	bool f = true;
	for(int j=0;j<5;j++)
	  if(board[i][j] == '.')f = false;
	if(f){
	  board.erase(board.begin()+i);
	  i--;
	}
      }
      /*
      for(int i=0;i<(int)board.size();i++)cout << board[i] << endl;
      cout << endl;
      */
    }
    int ans = 0;
    for(int i=0;i<(int)board.size();i++)
      for(int j=0;j<5;j++)
	if(board[i][j] == '#')ans++;
    cout << ans << endl;
  }
}