#include <bits/stdc++.h>

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = std::int64_t;

std::list<int> board;

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    while(true){
        int N;
        std::cin >> N;

        if(N == 0){
            break;
        }

        board.clear();

        for(int i=0;i<N;++i){
            int D, P, Q;
            std::cin >> D >> P >> Q;

            Q -= 1;

            if(D == 1){
                if(P == 5){
                    continue;
                }

                int block = ((1 << P) - 1) << Q;

                auto it = board.end();
                while(it != board.begin() &&
                      (*std::prev(it) & block) == 0){
                    std::advance(it, -1);
                }

                if(it == board.end()){
                    board.push_back(block);
                }else{
                    *it |= block;

                    if(*it == 31){
                        board.erase(it);
                    }
                }
            }else{
                auto it = board.end();
                while(it != board.begin() &&
                      (*std::prev(it) >> Q & 1) == 0){
                    std::advance(it, -1);
                }

                for(int i=0;i<P;++i){
                    if(it == board.end()){
                        board.push_back(1 << Q);
                    }else{
                        *it |= 1 << Q;
                        if(*it == 31){
                            it = board.erase(it);
                        }else{
                            std::advance(it, 1);
                        }
                    }
                }
            }
        }

        int cnt = 0;
        
        for(int l : board){
            cnt += __builtin_popcount(l);
        }

        std::cout << cnt << std::endl;
    }
}

