#include<bits/stdc++.h>
using namespace std;
const int LIMIT_HEIGHT = 5000;

int main()
{
  int n;
  bool grid[LIMIT_HEIGHT + 1][5];
  bool killed[LIMIT_HEIGHT + 1];

  while(cin >> n, n) {
    fill_n(*grid, (LIMIT_HEIGHT + 1) * 5, false);


    int d, p, q;
    while(n--) {
      cin >> d >> p >> q;
      --q;
      if(d == 1) { // 横向きのブロック
        for(int i = LIMIT_HEIGHT; i >= 0; i--) {
          bool flag = false;
          for(int j = 0; j < p; j++) {
            if(i == 0 || grid[i - 1][j + q]) {
              flag = true;
              for(int k = 0; k < p; k++) {
                grid[i][k + q] = true;
              }
              break;
            }
          }
          if(flag) break;
        }
        
      } else { // 縦向きのブロック
        for(int i = LIMIT_HEIGHT - p; i >= 0; i--) {
          if(i == 0 || grid[i - 1][q]) {
            for(int j = i; j < i + p; j++) {
              grid[j][q] = true;
            }
            break;
          }
        }
      }

      for(int i = 0; i < LIMIT_HEIGHT; i++) {
        bool flag = true;
        for(int j = 0; j < 5; j++) {
          flag &= grid[i][j];
        }
        killed[i] = flag;
      }
      int counter = 0;
      for(int i = 0; i < LIMIT_HEIGHT; i++) {
        while(i + counter < LIMIT_HEIGHT && killed[i + counter]) counter++;
     
        for(int j = 0; j < 5; j++) {
          if(i + counter >= LIMIT_HEIGHT) grid[i][j] = false;
          else grid[i][j] = grid[i + counter][j];
        }
      }
    }
    int ret = 0;
    for(int i = 0; i < LIMIT_HEIGHT; i++) {
      for(int j = 0; j < 5; j++) {
        ret += grid[i][j];
      }
    }
    cout << ret << endl;
  }
}