#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        list<bitset<5> > board(1, (1<<5)-1);
        while(--n >= 0){
            int d, p, q;
            cin >> d >> p >> q;
            bitset<5> bs;
            if(d == 1){
                for(int i=q-1; i<q-1+p; ++i)
                    bs[i] = true;
                p = 1;
            }else{
                bs[q-1] = true;
            }

            list<bitset<5> >::iterator it = board.begin();
            while(((*it) & bs).none())
                ++ it;

            while(--p >= 0){
                if(it == board.begin())
                    board.push_front(0);
                -- it;
                (*it) |= bs;
                if((*it) == (1<<5)-1)
                    it = board.erase(it);
            }
        }

        int ret = -5;
        list<bitset<5> >::iterator it;
        for(it=board.begin(); it!=board.end(); ++it)
            ret += it->count();
        cout << ret << endl;
    }
}