#define _USE_MATH_DEFINES
#include <iostream>
#include <complex>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <cmath>
#include <math.h>
#include <numeric>
#include <list>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <climits>
#include <set>
#include <memory.h>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <map>
#include <cassert>
#include <time.h>
#include <ctime>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int , P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 30;
const double EPS = 1e-9;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)

bool field[5005][5];

int block(int d, int p, int q){
	int x, y;
	if(d == 1) { x = p; y = 1;}
	else {y = p; x = 1;}
	for(int i = 5000; i >= 1; i--){
		bool f = false;
		rep(j, x){
			if(field[i-1][q+j]) f = true;
		}
		if(f){
			rep(j, y){
				rep(k, x){
					field[i+j][q+k] = true;
				}
			}
			return i;
		}
	}
	rep(i, y)rep(j, x) field[i][q+j] = true;
	return 0;
}

void mov(int pos){
	rep(i, 5) field[pos][i] = false;
	rep2(i, pos, 5000)rep(j, 5) swap(field[i+1][j], field[i][j]);
}

void update(int pos){
	int cnt = 0;
	for(int i = 4; i >= 0; i--){
		bool f = true;
		rep(j, 5){
			if(!field[pos+i][j]) f = false;
		}
		if(f) mov(pos + i);
	}
}

void drop(){
	int d, p, q;
	cin >> d >> p >> q;
	int pos = block(d, p, --q);
	update(pos);
}

int cnt(){
	int res = 0;
	rep(i, 5000)rep(j, 5) if(field[i][j]) res++;
	return res;
}

int main(){
	int n;
	while(cin >> n && n){
		memset(field, false, sizeof(field));
		rep(i, n){
			drop();
	//		cout << i << " " << cnt() << endl;
		}
		cout << cnt() << endl;
	}
	return 0;
}