// * template

#include <bits/stdc++.h>
#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif

using namespace std;

template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }
template<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }

template<class T, class U> inline void fill_array(T &e, const U &v) { e = v; }
template<class T, class U, size_t s> inline void fill_array(T (&a)[s], const U &v) {for(auto&e:a)fill_array(e,v);}
template<class T, class U, size_t s> inline void fill_array(array<T, s> &a, const U &v) {for(auto&e:a)fill_array(e,v);}
template<class T, class U> inline void fill_array(vector<T> &a, const U &v) {for(auto&e:a)fill_array(e,v);}

struct range {
	typedef int Int;
	struct iter {
		Int i;
		const Int s;
		constexpr iter(const Int &i_, const Int &s_):i(i_), s(s_) {}
		constexpr bool operator!=(const iter &r) const { return s > 0 ? i < r.i : i > r.i; }
		constexpr const Int &operator*() const { return i; }
		iter &operator++() { i += s; return *this; }
	};
	const Int f, l, s;
	constexpr range(const Int &f_, const Int &l_, const Int &s_):f(f_), l(l_), s(s_) {}
	constexpr range(const Int &f_, const Int &l_):f(f_), l(l_), s(1) {}
	constexpr range(const Int &num):f(0), l(num), s(1) {}
	constexpr iter begin() const { return iter(f, s); }
	constexpr iter end() const { return iter(l, s); }
};

// * solve

constexpr int MAX_NUM_BLOCK = 1000;
constexpr int MAX_BLOCK_SIZE = 5;
constexpr int WIDTH = 5;

bool field[MAX_NUM_BLOCK * MAX_BLOCK_SIZE + 1][WIDTH];
int next_pos[WIDTH];

void drop_block() {
	static constexpr int dx[] = {0, 1, 0};
	static constexpr int dy[] = {0, 0, 1};

	int d, p, q;
	cin >> d >> p >> q;

	int x = q - 1;
	int y = (d == 1 ? *max_element(begin(next_pos) + x, begin(next_pos) + x + p) : next_pos[x]);

	for(int i : range(p)) {
		field[y][x] = true;
		chmax(next_pos[x], y + 1);

		x += dx[d];
		y += dy[d];
	}
}

int calc_score() {
	const int h = *max_element(begin(next_pos), end(next_pos));

	int res = 0;
	for(int i : range(h)) {
		const int cnt = count(begin(field[i]), end(field[i]), true);
		if(cnt < WIDTH) res += cnt;
	}

	return res;
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	for(int n; cin >> n && n;) {
		memset(field, false, sizeof(field));
		memset(next_pos, 0, sizeof(next_pos));

		for(int i : range(n)) {
			drop_block();
		}

		cout << calc_score() << endl;
	}

	return EXIT_SUCCESS;
}