#include <iostream>
#include <algorithm>
#define H_MAX 10001
using namespace std;

void breakLine(int data[H_MAX][5], int y) {
  for(int i = y; i+1 < H_MAX; i++) {
    for(int j = 0; j < 5; j++) {
      data[i][j] = data[i+1][j];
    }
  }
}

void search(int data[H_MAX][5], int sy, int ey) {
  for(int i = sy; i <= ey; i++) {
    bool flag = true;
    for(int j = 0; j < 5; j++) {
      if(data[i][j] != 1) {
	flag = false;
	break;
      }
    }
    if(flag) {
      breakLine(data, i);
      i--;
    }
  }
}

int main() {
  while(1) {
    int n;
    cin >> n;
    if(n == 0) break;

    int data[H_MAX][5];

    for(int i = 0; i < H_MAX; i++) {
      for(int j = 0; j < 5; j++) {
	if(i == 0) data[i][j] = 1;
	else      data[i][j] = 0;
      }
    }

    for(int i = 0; i < n; i++) {
      int d, p, q, qy;
      cin >> d >> p >> q;
      q--;
      for(int y = H_MAX-1; y >= 0; y--) {
	bool put = false;
	switch(d) {
	case 1:
	  for(int j = 0; j < p; j++) {
	    if(data[y][q+j] == 1) {
	      put = true;
	      y++;
	      qy = y;
	      for(int k = 0; k < p; k++) {
		data[y][q+k] = 1;
	      }
	      break;
	    }
	  }
	  break;
	case 2:
	  if(data[y][q] == 1) {
	    put = true;
	    y++;
	    qy = y;
	    for(int j = 0; j < p; j++) {
	      data[y+j][q] = 1;
	    }
	  }
	  break;
	}
	if(put) {
	  break;
	}
      }

      switch(d) {
      case 1: search(data, qy, qy); break;
      case 2: search(data, qy, qy+p-1); break;
      }
    }

    int count = 0;
    for(int i = 1; i < H_MAX; i++) {
      for(int j = 0; j < 5; j++) {
	count += data[i][j];
      }
    }
    cout << count << endl;
  }
  return 0;
}