Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)
var (
	dat = [5000][5]int{}
	m = [5]int{}
)

func main() {
	for scanner.Scan() {
		for i := range dat {
			for j := range dat[i] {
				dat[i][j] = 0
			}
		}
		for i := range m {
			m[i] = 0
		}
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		count := 0
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			d, _ := strconv.Atoi(buf[0])
			p, _ := strconv.Atoi(buf[1])
			q, _ := strconv.Atoi(buf[2])
			q--
			count += p
			if d == 2 {
				for j := 0; j < p; j++ {
					dat[m[q]][q] = 1
					ok := true
					for k := 0; k < 5; k++ {
						if dat[m[q]][k] == 0 {
							ok = false
						}
					}
					if ok {
						for k := 0; k < 5; k++ {
							dat[m[q]][k] = 0
							for l := m[q]; l < 4999; l++ {
								dat[l][k] = dat[l+1][k]
							}
							for l := 4999; l >= 0; l-- {
								if dat[l][k] != 0 {
									m[k] = l+1
									break
								}
								if l == 0 {
									m[k] = 0
								}
							}
						}
						count -= 5
					} else {
						m[q]++
					}
				}
			} else {
				h := 0
				for i := 0; i < p; i++ {
					h = max(h, m[i+q])
				}
				for i := 0; i < p; i++ {
					dat[h][i+q] = 1
				}
				for i := 0; i < p; i++ {
					m[i+q] = h+1
				}
				ok := true
				for j := 0; j < 5; j++ {
					if dat[h][j] == 0 {
						ok = false
					}
				}
				if ok {
					for j := 0; j < 5; j++ {
						dat[h][j] = 0
						for k := h; k < 4999; k++ {
							dat[k][j]=dat[k+1][j]
						}
						for k := 4999; k>= 0; k-- {
							if dat[k][j] != 0 {
								m[j]=k+1
								break
							}
							if k == 0 {
								m[j] = 0
							}
						}
					}
					count -= 5
				}
			}
		}
		fmt.Println(count)
	}
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

Python: end = [1,1,1,1,1]
while True:
  n = int(input())
  if n == 0:
    break
  mp = [[0] * 5 for _ in range(6000)]
  height = [0] * 5
  cnt = 0
  for i in range(n):
    d, p, q = map(int, input().split())
    q -= 1
    cnt += p
    if d == 1:
      pos = max(height[q:q + p])
      mp[pos][q:q + p] = [1] * p
      if mp[pos] == end:
        cnt -= 5
        mp.pop(pos)
        mp.append([0] * 5)
        for x in range(5):
          for y in range(i * 5 - 1, -1, -1):
            if mp[y][x] == 1:
              height[x] = y + 1
              break
          else:
            height[x] = 0
      else:
        height[q:q + p] = [pos + 1] * p

    if d == 2:
      pop_flag = False
      pos = height[q]
      for y in range(pos, pos + p):
        mp[y][q] = 1
      for y in range(pos + p - 1, pos - 1, -1):
        if mp[y] == end:
          cnt -= 5
          mp.pop(y)
          mp.append([0] * 5)
          pop_flag = True
      
      if pop_flag:
        for x in range(5):
          for y in range(i * 5 - 1, -1, -1):
            if mp[y][x] == 1:
              height[x] = y + 1
              break
          else:
            height[x] = 0
      else:
        height[q] += p
  
  print(cnt)

Python: while True:
  n = int(input())
  if n == 0:
    break

  mp = [[0] * 5 for _ in range(5000)]
  depth = [0, 0, 0, 0, 0]
  ans = 0
  for _ in range(n):
    d, p, q = map(int, input().split())
    ans += p

    if d == 1:
      max_depth = max(depth[q-1:q-1+p])
      for i in range(q - 1, q - 1 + p):
        mp[max_depth][i] = 1
        depth[i] = max_depth + 1
    else:
      max_depth = depth[q - 1]
      for i in range(max_depth, max_depth + p):
        mp[i][q - 1] = 1
      depth[q - 1] = max_depth + p
  
  max_depth = max(depth)
  cnt = 0
  for depth in range(max_depth):
    if mp[depth] == [1,1,1,1,1]:
      cnt += 1
  print(ans - 5 * cnt)

Python: N = int(input())

while True:
    ls = [[None] * 5] * 5000
    flwr = 0
    for _ in xrange(N):
        d, l, pos = map(int, raw_input().split())
        # block
        if d == 1:
            for i in xrange(l):
                ls[flwr][pos].append(1)
        else:
            tmp = 0
            for i in xrange(l):
                ls[flwr][pos+tmp].append(1)
                tmp += 1
        # up flower
        if not None in ls[0]:
            flwr += 1
    ret = 0
    for i in xrange(flwr):
        ret += len([i for i in ls[i] if not None])

    N = int(input())
    # end condition
    if not N:
        break

Python: def v_block():
    block.insert(0, [' ']*5)
    for l, line in enumerate(block):
        if line[q-1:q+p-1] != [' ']*p:
            break
    else:
        l += 1
    block[l-1][q-1:q+p-1] = ['*']*p
    return delete()


def h_block():
    for _p in range(p):
        block.insert(0, [' ']*5)
    for l, line in enumerate(block):
        if line[q-1] == '*':
            break
    else:
        l += 1
    for _l in range(l-1, l-p-1, -1):
        block[_l][q-1] = '*'
    return delete()
                

def delete():
    b = len(block)
    l = 0
    while 0 < b:
        if block[l] == [' ']*5 or block[l] == ['*']*5:
            del block[l]
        else:
            l += 1
        b -= 1


def count():
    x = 0
    for line in block:
        for b in line:
            if b == '*':
                x += 1
    else:
        print(x)


while True:
    n = int(input())
    if n == 0:
        break
    block = [[' ']*5]
    for i in range(n):
        d, p, q = map(int, input().split())
        if d == 1:
            v_block()
        else:
            h_block()
    else:
        count()

C++: #define _USE_MATH_DEFINES
#define MAX_N 1000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 1e-8;

int gStage[6000][5];

void putBlock(int dir,int len,int pos){
	if(dir == 1){
		for(int i=6000-1;i>=0;i--){
			bool isok=true;
			for(int j=pos;j<pos+len;j++){
				if(gStage[i][j]==1) isok=false;
			}
			
			if(!isok){
				for(int j=pos;j<pos+len;j++){
					gStage[i+1][j] = 1;
				}
				return;
			}

			else if(isok && i==0){
				for(int j=pos;j<pos+len;j++){
					gStage[i+1][j] = 1;
				}
				return;
			}
		}
	}
	else{
		for(int i=6000-1;i>=0;i--){
			bool isok=true;
			if(gStage[i][pos]==1) isok=false;

			if(!isok){
				for(int j=i+1;j<i+1+len;j++){
					gStage[j][pos] = 1;
				}
				return;
			}

			else if(isok && i==0){
				for(int j=i;j<i+len;j++){
					gStage[j][pos] = 1;
				}
				return;
			}
		}
	}
}

int countBlock(){
	int res=0;
	for(int i=0;i<6000;i++){
		for(int j=0;j<5;j++){
			res += gStage[i][j];
		}
	}

	return res;
}

void eraseBlock(){
	for(int i=0;i<6000;i++){
		bool isok=true;
		for(int j=0;j<5;j++){
			if(gStage[i][j]==0) isok = false;
		}

		if(isok){
			for(int j=i;j+1<6000;j++){
				for(int k=0;k<5;k++){
					gStage[j][k] = gStage[j+1][k];
				}
			}
			i--;
		}
	}
}

void printBlock(){
	for(int i=0;i<50;i++){
		for(int j=0;j<5;j++){
			printf("%d",gStage[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

int main(){
	int n;
	while(~scanf("%d",&n)){
		if(n==0) break;

		memset(gStage,0,sizeof(gStage));
		for(int i=0;i<n;i++){
			int dir,len,pos;
			scanf("%d %d %d",&dir,&len,&pos);
			pos-=1;
			putBlock(dir,len,pos);
			//printBlock();
			eraseBlock();
			//printBlock();
		}

		printf("%d\n",countBlock());
	}
}