#include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <numeric>
#include <utility>
#include <tuple>

int const yoko = 1;
int const tate = 2;

void tetris(std::vector<std::vector<int>>& z) {
  size_t m = z[0].size();
  std::vector<std::vector<int>> y(5, std::vector<int>(m));

  size_t i = 0;
  for (size_t j = 0; j < m; ++j) {
    int cur = 0;
    for (size_t k = 0; k < 5; ++k) cur += z[k][j];
    if (cur == 5) continue;
    for (size_t k = 0; k < 5; ++k) y[k][i] = z[k][j];
    ++i;
  }
  z = std::move(y);
}

int testcase_ends() {
  size_t n;
  scanf("%zu", &n);
  if (n == 0) return 1;

  size_t m = 5*n+10;
  std::vector<std::vector<int>> z(5, std::vector<int>(m));
  for (size_t i = 0; i < n; ++i) {
    size_t d, p, q;
    scanf("%zu %zu %zu", &d, &p, &q);
    --q;

    size_t l = q;
    size_t r = q;
    if (d == yoko) r += p-1;

    size_t h = 0;
    for (size_t j = l; j <= r; ++j) {
      for (size_t k = m; k--;) {
        if (z[j][k]) {
          h = std::max(h, k+1);
          break;
        }
      }
    }

    if (d == yoko) {
      for (size_t i = l; i <= r; ++i) ++z[i][h];
    } else if (d == tate) {
      for (size_t i = 0; i < p; ++i) ++z[q][h+i];
    }

    tetris(z);
  }
  int res = 0;
  for (auto const& zi: z)
    res += std::accumulate(zi.begin(), zi.end(), 0);

  printf("%d\n", res);
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}

