#include<iostream>
#include<cstring>

using namespace std;

const int Height = 5500;
const int Width = 5;
char field[Height][Width];
int top;
int dx[] = {1,0};
int dy[] = {0,-1};


void print(int h, int w){

  for(int i = 0; i < h; i++){
    for(int j = 0; j < w; j++) cout << field[i][j];
    cout << endl;
  }
  cout << endl;
    
}
bool isInside(int h, int w){return 0<=h&&h<Height&&0<=w&&w<Width;}

int getH(int d, int len, int pos){
  for(int i = Height-1; i>=0; i--){
    int h = i;
    int w = pos;
    for(int j = 0; j < len; j++){
      if(field[h][w] != '?' || !isInside(h,w)) return i+1;
      h += dy[d];
      w += dx[d];
    }
  }
}

void put(int d, int len, int pos){

  int nex = getH(d,len,pos);
  top = max(top,nex);

  int h = nex;
  int w = pos;

  for(int i = 0; i < len; i++){
    field[h][w] = '1';
    h += dy[d];
    w += dx[d];
  }
}

void count(){


  int sum = 0;
  
  for(int i = 0; i < Height; i++){
    int tmp = 0;
    for(int j = 0; j < Width; j++){
      if(field[i][j] != '?') tmp++;
    }
    if(tmp != 5) sum += tmp;
  }
  cout << sum << endl;
}

int main(){

  int n;
  while(cin >> n && n){
    memset(field,'?',sizeof(field));
    top = 0;

    while(n--){
      int d, len, pos;
      cin >> d >> len >> pos;
      put(d-1,len,pos-1);
    }
    count();
    //    print(10,5);
  }
  return 0;
}