#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <map>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<30;

int main() {
  int n;
  while(cin >> n, n) {
    int height[5] = {}; // ツ各ツ療アツづ可堕カツ催敖つキツづゥツブツδ債ッツクツづ個暗ェツ氾板湘」ツづ個暗ェツ古つ湘」
    bool ba[5][5010] = {};

    while(n--) {
      int d, p, q;
      cin >> d >> p >> q;
      q--;
      if (d == 1) {
        int ma = 0;
        for (int i=q; i<p+q; ++i) {
          ma = max(ma, height[i]);
        }
        for (int i=q; i<p+q; ++i) {
          ba[i][ma] = 1;
          height[i] = ma + 1;
        }
        bool f = 1;
        REP(i,5)
          if (!ba[i][ma])
            f = 0;
        if (f) {
          REP(i,5) {
            for (int j=ma+1; j<height[i]; ++j)
              if (j>0)
                ba[i][j-1] = ba[i][j];
            ba[i][height[i]-1] = 0;
            while(height[i] > 0 && !ba[i][height[i]-1])
              height[i]--;
          }
        }
      } else {
        REP(i,p) {
          ba[q][height[q]+i] = 1;
        }
        height[q] += p;
        for (int k=height[q]-p; k<height[q]; ++k) {
          bool f = 1;
          REP(i,5)
            if (!ba[i][k])
              f = 0;
          //    cout << k << " " << f << endl;
          if (f) {
            REP(i,5) {
              for (int j=k+1; j<height[j]; ++j) {
                if (j > 0)
                  ba[i][j-1] = ba[i][j];
              }
              ba[i][height[i]-1] = 0;
              while(height[i] > 0 && !ba[i][height[i]-1])
                height[i]--;
            }
            // REP(i,10) {
            //   REP(j,5) {
            //     cout << ba[j][9-i];
            //   }
            //   cout << endl;
            // }
            // cout << endl;
            k--;
          }
        }
      }
      // REP(i,10) {
      //   REP(j,5) {
      //     cout << ba[j][9-i];
      //   }
      //   cout << endl;
      // }
      // REP(i,5)
      //   cout << height[i] << " ";
      // cout << endl << endl;;
    }
    int ans = 0;
    REP(i,5)
      REP(j,5010)
        if (ba[i][j])
          ans++;
    cout << ans << endl;
  }
}