#include<iostream>
#include<vector>
#include<algorithm>
#include<bitset>


using namespace std;
#define rep(i,n) for(int i = 0 ; i < n ; ++i)



int solve(){
    int n;
    cin>>n;
    if(n==0)return 0;
    vector<int> res(5000);
    rep(i,n){
        int d,p,q;
        cin>>d>>p>>q;
        if(d==1){
            int bit = ((1<<p)-1)<<(q-1);
            int i = res.size();
            while(i>0&&(res[i-1]&bit)==0)--i;
            res[i]|=bit;
            if(res[i]==31){
                res.erase(res.begin()+i);
            }
        }
        else {
            int i = res.size();
            int bit = 1<<(q-1);
            while(i>0&&(res[i-1]&bit)==0)--i;
            rep(j,p)res[i+j]|=bit;
            for(int j=p-1;j>=0;--j){
                if(res[i+j]==31)res.erase(res.begin()+i+j);
            }
        }
    }
    int ans = 0;
    for(auto e : res){
        ans += __builtin_popcount(e);
    }
    cout<<ans<<endl;
    return 1;
}

int main(){
    while(solve());
}
