#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <functional>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <utility>
#include <vector>

using namespace std;

typedef long long Long;
#define Whole(xs) xs.begin(), xs.end()

template<class T>
ostream& operator<<(ostream& os, const vector<T>& vs) {
    if (vs.empty()) return os << "[]";
    os << "[" << vs[0];
    for (int i = 0; i < vs.size(); i++) os << " " << vs[i];
    return os << "]";
}
ostream& operator<<(ostream& os, const vector<int>& vs) {
    if (vs.empty()) return os << "[]";
    bool flag = false;
    os << "[";
    for (int i = 0; i < vs.size(); i++) {
        if (flag) {
            os << " ";
        } else {
            flag = true;
        }
        for (int j = 0; j < 5; j++) {
            os << ( (vs[i] & (1 << j)) > 0 );
        }
    }
    return os << "]";
}

const int IINF = 1<<28;
const Long LINF = 1LL<<56;
#define INF IINF

int N;
vector<int> F;
bool Input() {
    cin >> N;
    if (N == 0 || cin.eof()) return false;
    F.clear();
    return true;
}

void WSet(int Q, int P) {
    int R = 0;
    for (int i = Q; i < Q + P; i++) {
        R ^= (1 << i);
    }
    if (F.empty()) {
        F.push_back(R);
    } else{
        if ((F.back() & R) == 0) {
            for (int i = F.size() - 1; i >= -1; i--) {
                if ( i < 0 || (F[i] & R) > 0 ) {
                    F[i + 1] ^= R;
                    break;
                }
            }
        } else {
            F.push_back(R);
        }
    }
}

void HSet(int Q, int P) {   
    if (F.empty()) {
        for (int i = 0; i < P; i++) {
            F.push_back(1 << Q);
        }
        return;
    }
    int Rem = P;
    for (int i = F.size() - 1; i >= -1; i--) {
        if (i < 0 || F[i] & (1 << Q)) {
            for (int j = i + 1; Rem > 0 && j < F.size(); j++) {
                Rem--;
                F[j] ^= (1 << Q);
            }
            break;
        }
    }
    for (int i = 0; i < Rem; i++) {
        F.push_back(1 << Q);
    }
}

void Fall() {
    vector<int> nF;
    for (int i = 0; i < F.size(); i++) {
        if (F[i] != (1 << 5) - 1) {
            nF.push_back(F[i]);
        }
    }
    F = nF;
}

void Solve() {
    for (int n = 0; n < N; n++) {
        int D, P, Q; cin >> D >> P >> Q;
        Q--;
        if (D == 1) {
            WSet(Q, P);
        } else {
            HSet(Q, P);
        }
        Fall();
    }
    int Ans = 0;
    for (int i = 0; i < F.size(); i++) {
        for (int j = 0; j < 5; j++) {
            if (F[i] & (1 << j)) {
                Ans++;
            }
        }
    }
    cout << Ans << endl;
}

int main() {
    while (Input()) {
        Solve();
    }
    return 0;
}