#include <bits/stdc++.h>

using namespace std;

char stage[2][5000][5];

int main()
{
    int n;
    
    while (scanf("%d", &n) && n){
        int height[5] = {0};
        int turn = 0;
        int ct = 0;
        memset(stage, 0, sizeof(stage));
        for (int rep = 0; rep < n; rep++){
            int d, p, q;
            scanf("%d %d %d", &d, &p, &q); --q;
            if (d == 1){
                int mh = *max_element(height + q, height + p + q);
                for (int j = q; j < p + q; j++){
                    stage[turn][mh][j] = 1;
                    height[j] = mh + 1;
                }
            }
            else {
                for (int j = 0; j < p; j++){
                    stage[turn][height[q]++][q] = 1;
                }
            }
            
            int look = *max_element(height, height + 5);
            memset(stage[1 - turn], 0, sizeof(stage[1 - turn]));
            memset(height, 0, sizeof(height));
            ct = 0;
            int nh = 0;
            for (int i = 0; i < look; i++){
                bool ok = true;
                for (int j = 0; j < 5; j++){
                    if (stage[turn][i][j] == 0) ok = false;
                }
                if (!ok){
                    for (int j = 0; j < 5; j++){
                        stage[1 - turn][nh][j] = stage[turn][i][j];
                        if (stage[1 - turn][nh][j]){
                            height[j] = nh + 1;
                            ct++;
                        }
                    }
                    nh++;
                }
            }
            turn = 1 - turn;
        }
        
        printf("%d\n", ct);
    }
    
    return (0);
}