#define _USE_MATH_DEFINES
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <iterator>
 
using namespace std;
 
typedef long long ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
 
#define FOR(i,n) for(int i = 0; i < (n); i++)
#define sz(c) ((int)c.size())

typedef unsigned long long ull;
const int INF = (int)1e8;

bool t[5*1000][5];

int GetUnder(int x){
	for(int i = 5000-1; i >= 0; i--){
		if(t[i][x]) return i+1;
	}
	return 0;
}

int GetUnder(int d,int p,int q){
	int under = -1;
	if(d==2){
		under = GetUnder(q);
	}
	else {
		FOR(i,p){
			under = max(under,GetUnder(q+i));
		}
	}
	return under;
}

int main(){
	int n;
	while(cin>>n,n){
		memset(t,0,sizeof(t));
		FOR(i,n){
			int d,p,q; cin>>d>>p>>q;
			q--;

			int under = GetUnder(d,p,q);

			if(d==2){
				FOR(i,p) t[under+i][q] = true;
			} else {
				FOR(i,p) t[under][q+i] = true;
			}

			int c = 0;
			FOR(i,5000){
				if(accumulate(t[i],t[i]+5,0) == 5) continue;
				memcpy(t[c++],t[i],5);
			}
			for(int i = c; i < 5000; i++) FOR(j,5) t[i][j] = false;
		}
		int ans = 0;
		FOR(i,5000) FOR(j,5) ans += t[i][j];

		cout << ans << endl;
	}
	return 0;
}