#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <list>

using namespace std;

#define inf 1e9 + 7

/* 
typedef struct Node {
  int id;
  int score;
  Node *right;
  Node *left;
};
*/

vector<vector<int>> grid(5010, vector<int>(5, 0));
vector<int> ma(5, 0);

void check(int start , int end){
  // endは含まれない

  for(int i = end - 1; i >= start; i--){
    bool vanish = true;
    for(int j = 0; j < 5; j++){
      if(grid[i][j] != 1){
        vanish = false;
        break;
      }
    }
    if(vanish){
      //cout << "消す " << i << "行目" << endl;
      // 消す
      for(int j = 0; j < 5; j++){
        grid[i][j] = inf;
        // 最大値更新

        if(ma[j] == i + 1){

          // 下の1まで下がる
          for(int k = i - 1; k >= 0; k--){
            if(grid[k][j] == 1){
              ma[j] = k + 1;
              break;
            }
            if(k == 0) ma[j] = 0; 
          }

        }

      }

    }
  }
}

int main(){

  while(1){
    int n;
    cin >> n;
    if(n == 0) break;

    // 初期化
    for(int i = 0; i < 5010; i++){
      for(int j = 0; j < 5; j++){
        grid[i][j] = 0;
      }
    }
    for(int j = 0; j < 5; j++) ma[j] = 0;

    vector<int> d(n), p(n), q(n);
    for(int i = 0; i < n; i++) cin >> d[i] >> p[i] >> q[i];

    for(int i = 0; i < n; i++){
      if(d[i] == 1){ // 横
        int ster = 0;
        for(int j = q[i] - 1; j < q[i] - 1 + p[i]; j++){
          ster = max(ster,ma[j]);
        }
        // sterのとこにおく
        while(grid[ster][q[i] - 1] == inf){
          ster++;
        }
        for(int j = q[i] - 1; j < q[i] - 1 + p[i]; j++){
          grid[ster][j] = 1;
          ma[j] = ster + 1;
        }
        check(ster, ster + 1);
      }
      else{ // 縦
        int ster = ma[q[i] - 1];
        while(grid[ster][q[i] - 1] == inf) ster++;
        int cnt = 0;
        for(int j = 0; j < p[i]; j++){
          if(grid[ster+j+cnt][q[i] - 1] == inf){
            cnt++;
            j--;
            continue;
          }
          grid[ster + cnt + j][q[i] - 1] = 1;
        }
        ma[q[i] - 1] = ster + p[i] + cnt;
        check(ma[q[i] - 1] - p[i] - ster - cnt, ma[q[i] - 1] + cnt);
      }
    }

    int ans = 0;
    for(int i = 5010 - 1; i >= 0; i--){
      for(int j = 0; j < 5; j++){
        //cout << grid[i][j] << " ";
        if(grid[i][j] == 1) ans++;
      }
      //cout << endl;
    }

    cout << ans << endl;
  }

  return 0;
}
