#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int d[1010], p[1010], q[1010];
int f[5010][5];

bool cant(int d, int p, int q, int r) {
  if (d == 1) {
    for (int i = 0; i < p; i++) {
      if (f[r][q + i]) return true;
    }
  } else {
    if (f[r][q]) return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int n;
  while (cin >> n, n) {
    memset(f, 0, sizeof(f));
    for (int i = 0; i < n; i++) {
      cin >> d[i] >> p[i] >> q[i];
      q[i]--;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 4999; ; j--) {
        if (j < 0 || cant(d[i], p[i], q[i], j)) {
          if (d[i] == 1) {
            for (int k = 0; k < p[i]; k++) {
              f[j + 1][q[i] + k] = 1;
            }
          } else {
            for (int k = 0; k < p[i]; k++) {
              f[j + 1 + k][q[i]] = 1;
            }
          }
          break;
        }
      }
      bool update = true;
      while (update) {
        update = false;
        for (int j = 0; j < 5000; j++) {
          bool kieru = true;
          for (int k = 0; k < 5; k++) {
            kieru &= f[j][k];
          }
          if (kieru) {
            update = true;
            for (int k = 0; k < 5; k++) {
              f[j][k] = 0;
            }
            for (int k = j; k < 5000; k++) {
              for (int l = 0; l < 5; l++) {
                swap(f[k][l], f[k + 1][l]);
              }
            }
          }
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < 5000; i++) {
      for (int j = 0; j < 5; j++) {
        ans += f[i][j];
      }
    }
    cout << ans << endl;
  }

  return 0;
}
