#include <iostream>
#include <list>
#include <cstring>

using namespace std;

int main(int argc, char *argv[]) {
  list<char *>::iterator itr, bef;
  list<char *> space;
  int n, b, p, q;
  while (true) {
    space.push_back(new char[5]);
    itr = space.begin();
    memset(*itr, 0, sizeof(char)*5);
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> b >> p >> q;
      itr = space.begin();
      q--;
      if (b == 1) {//yoko                                                                                                                                                                                                                                                                                                                           
        while (true) {
          bool avail = true;
          for (int j = 0; j < p; j++)
            if((*itr)[q+j])
              avail = false;
          if (avail) {
            bef = itr;
            itr++;
            if (itr == space.end()) {
              for (int j = 0; j < p; j++)
                (*bef)[q+j] = 1;
              break;
            }
          } else if (!avail && itr == space.begin()) {
            space.push_front(new char[5]);
            bef = space.begin();
            memset(*bef, 0, sizeof(char)*5);
          } else {
            for (int j = 0; j < p; j++)
              (*bef)[q+j] = 1;
            break;
          }
        }
      } else {//tate                                                                                                                                                                                                                                                                                                                                
        bef = itr;
        while (!(*itr)[q] && itr != space.end())
          itr++;
        list<char *>::iterator tmp = itr;
        while (p > 0) {
          if (itr == space.begin())
            break;
          itr--;
          p--;
          (*itr)[q] = 1;
        }
        for (int j = 0; j < p; j++) {
          space.push_front(new char[5]);
          bef = space.begin();
          memset(*bef, 0, sizeof(char)*5);
          (*bef)[q] = 1;
        }
        itr = tmp;
      }
      while (bef != itr) {
        if ((*bef)[0] && (*bef)[1] && (*bef)[2] &&
            (*bef)[3] && (*bef)[4]) {
          list<char *>::iterator tmp = bef;
          tmp++;
          space.erase(bef);
          bef = tmp;
        } else {
          bef++;
        }
      }
    }
    int result = 0;
    for (list<char *>::iterator prt = space.begin();
         prt != space.end(); prt++) {
      for (int j = 0; j < 5; j++) {
        //cout << (int)(*prt)[j];                                                                                                                                                                                                                                                                                                                   
        if ((*prt)[j])
          result++;
      }
      /*if (prt == bef)                                                                                                                                                                                                                                                                                                                             
        cout << "<-bef";                                                                                                                                                                                                                                                                                                                            
      else if (prt == itr)                                                                                                                                                                                                                                                                                                                          
        cout << "<-itr";                                                                                                                                                                                                                                                                                                                            
        cout << endl;*/
    }
    cout << result << endl;
    space.clear();
  }
  return 0;
}