#include <bits/stdc++.h>

using namespace std;

char stage[2][5001][5];

void place(int *height, int col, int d, int len, int pos, int turn)
{
    if (d == 1){
        int mh = *max_element(height + pos, height + pos + len);
        for (int j = pos; j < pos + len; j++){
            stage[turn][mh][j] = col;
            height[j] = mh + 1;
        }
    }
    else {
        for (int j = 0; j < len; j++){
            stage[turn][height[pos]++][pos] = col;
        }
    }
    return;
}

int main()
{
    int n;
    
    while (scanf("%d", &n) && n){
        int height[5] = {0};
        int d[1001] = {0}, len[1001] = {0}, pos[1001] = {0};
        
        memset(stage, 0, sizeof(stage));
        
        for (int i = 1; i <= n; i++){
            scanf("%d %d %d", d + i, len + i, pos + i); --pos[i];
            place(height, i, d[i], len[i], pos[i], 0);
        }
        
        int turn = 0;
        for (int i = 0; i < n; i++){
            int ht = *max_element(height, height + 5);
            
            for (int j = 0; j < ht; j++){
                bool ok = true;
                for (int k = 0; k < 5; k++){
                    stage[1 - turn][j][k] = 0;
                    if (!stage[turn][j][k]) ok = false;
                }
                if (ok){
                    for (int k = 0; k < 5; k++){
                        len[stage[turn][j][k]]--;
                    }
                }
            }
            
            memset(height, 0, sizeof(height));
            for (int j = 1; j <= n; j++){
                place(height, j, d[j], len[j], pos[j], 1 - turn);
            }
            turn = 1 - turn;
        }
        
        int ret = 0;
        int h = *max_element(height, height + 5);
        for (int i = 0; i < h; i++){
            for (int j = 0; j < 5; j++){
                ret += !!stage[turn][i][j];
            }
        }
        
        printf("%d\n", ret);
    }
    
    return (0);
}