#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }



/* Header */
enum Color{ CLEAR, BLACK };
struct Point;
class Block;
class Board;

struct Point
{
	int x, y;
	
	Point() : x(0), y(0) {}
	Point(int v) : x(v), y(v) {}
	Point(int x, int y) : x(x), y(y) {}
};

Point operator + (const Point& left, const Point& right)
{	return Point(left.x + right.x, left.y + right.y);	}

class Block
{
public:

	Color color;
	
	// points[0]ÌÝâÎÀWA»êÈOÍpoints[0]©çÌÎÀW
	vector<Point> points;
	
	Block(vector<Point> points, Color color)
		: points(points), color(color) {}
};

class Board
{
	int width, height;
	vector<vector<Color> > board;
	
public:
	
	Board(int width, int height);
	
	bool BoardRange(Point point);
		
	bool PutBlock(const Block& block, bool twice = false);
	
	bool RemoveBlock(const Block& block);
	
	bool DownBlock(Block *block);
	
	int DeleteLine();
	
	int CountBlock();
};



/* Implement */
Board::Board(int width, int height)
	: width(width), height(height)
{
	board = vector<vector<Color> >(height, vector<Color>(width, CLEAR));
}

bool Board::BoardRange(Point point)
{
	return 0 <= point.x && point.x < width && 0 <= point.y && point.y < height;
}

bool Board::PutBlock(const Block& block, bool twice)
{
	for(int i = 0; i < block.points.size(); i++)
	{
		Point p = (i ? block.points[0] : Point(0, 0)) + block.points[i];
		
		if(twice) board[p.y][p.x] = block.color;
		else if(!BoardRange(p) || board[p.y][p.x] != CLEAR) return false;
	}
	
	if(!twice) PutBlock(block, true);
	
	return true;
}

bool Board::RemoveBlock(const Block& block)
{
	for(int i = 0; i < block.points.size(); i++)
	{
		Point p = (i ? block.points[0] : Point(0, 0)) + block.points[i];
		
		board[p.y][p.x] = CLEAR;
	}
}

bool Board::DownBlock(Block *block)
{
	RemoveBlock(*block);
	
	block->points[0].y--;
	
	if(PutBlock(*block))
	{
		return true;
	}
	else
	{
		block->points[0].y++;
		PutBlock(*block);
		
		return false;
	}
}

int Board::DeleteLine()
{
	int ret = 0;
	
	for(int i = height - 1; 0 <= i; i--)
	{
		if(count(board[i].begin(), board[i].end(), CLEAR) == 0)
		{
			board[i] = vector<Color>(width, CLEAR);
			
			for(int j = i; j < height - 1; j++)
			{
				board[j] = board[j + 1];
			}
			
			ret++;
		}
	}
	
	return ret;
}

int Board::CountBlock()
{
	int ret = 0;
	
	for(int y = 0; y < height; y++)
	{
		for(int x = 0; x < width; x++)
		{
			ret += board[y][x] != CLEAR;
		}
	}
	
	return ret;
}



/* Entry */
int main()
{
    int n;
	while(cin >> n && n)
	{		
		Board board(5, 5048);
		
		rep(i, n)
		{
			int d, p, q;
			cin >> d >> p >> q;
			
			vector<Point> points; points.push_back(Point(q - 1, 5000));
			REP(j, 1, p)  points.push_back(Point((d == 1 ? j : 0), (d == 2 ? j : 0)));
			
			Block block(points, BLACK);
			while(board.DownBlock(&block));
			board.DeleteLine();
		}
		
		cout << board.CountBlock() << endl;
	}
}