#include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>
enum class Direction {
	Horizontal, Vertical
};
Direction as_direction(int d) {
	switch (d) {
	case 1: return Direction::Horizontal;
	case 2: return Direction::Vertical;
	default: throw 0;
	}
}
struct Block {
	Direction direction;
	int length;
	int position;
	int height() const {
		switch (direction) {
		case Direction::Horizontal: return 1;
		case Direction::Vertical: return length;
		default: throw 0;
		}
	}
	int width() const {
		switch (direction) {
		case Direction::Horizontal: return length;
		case Direction::Vertical: return 1;
		default: throw 0;
		}
	}
};
struct Line {
	int state = 0;
	void write(Block block) {
		state |= (1 << (block.position + block.width())) - (1 << block.position);
	}
	bool can_put(Block block) const {
		return (state & ((1 << (block.position + block.width())) - (1 << block.position))) == 0;
	}
	bool is_filled() const {
		return state + 2 == (1 << 6);
	}
};
struct Field {
	std::list<Line> list;
	int block_rest = 0;
	void drop_block(Block block) {
		block_rest += block.height() * block.width();
		auto last = std::find_if(list.rbegin(), list.rend(), [block](const Line& line) {return !line.can_put(block); });
		if (last == list.rend()) {
			while (list.size() < block.height()) {
				list.push_back(Line());
			}
			for (auto itr = list.begin(); std::distance(list.begin(), itr) < block.height(); ++itr) {
				itr->write(block);
			}
			auto itr = list.begin();
			for (auto i = 0; i < block.height(); ++i) {
				if (itr->is_filled()) {
					itr = list.erase(itr);
					block_rest -= 5;
				}
				else {
					++itr;
				}
			}
		}
		else if (last == list.rbegin()) {
			for (auto i = 0; i < block.height(); ++i) {
				auto line = Line();
				line.write(block);
				list.push_back(line);
			}
			for (auto itr = std::prev(list.end(), block.height()); itr != list.end();) {
				if (itr->is_filled()) {
					itr = list.erase(itr);
					block_rest -= 5;
				}
				else {
					++itr;
				}
			}
		}
		else {
			auto rev_count = std::distance(list.rbegin(), last);
			for (; rev_count < block.height(); ++rev_count) {
				list.push_back(Line());
			}
			auto itr = std::prev(list.end(), rev_count);
			for (auto i = 0; i < block.height(); ++i) {
				itr->write(block);
				if (itr->is_filled()) {
					itr = list.erase(itr);
					block_rest -= 5;
				}
				else {
					++itr;
				}
			}

		}
	}
	int block_count() const { return block_rest; }
};
int main() {
	while (true) {
		int n; std::cin >> n; if (n == 0) break;
		Field field;
		for (auto i = 0; i < n; ++i) {
			int d, l, p; std::cin >> d >> l >> p;
			Block block{ as_direction(d), l, p };
			field.drop_block(block);
		}
		std::cout << field.block_count() << '\n';
	}
}
