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: N = raw_input()

while True:
    ls = [[None] * 5] * 5000
    flwr = 0
    for _ in xrange(N):
        d, l, pos = 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
    N = raw_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():
    l = 0
    while True:
        if block[l] == [' ']*5 or block[l] == ['*']*5:
            del block[l]
        else:
            l += 1
        if len(block) == l:
            return


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()

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

    table = [[0 for i in range(5)] for j in range(6000)]
    blocks = [0 for i in range(5)]
    for l in range(N):
        d,p,q = [int(i) for i in input().split()]
        q -= 1
        if d == 1:
            maxtop = 0
            for i in range(q, q+p):
                cnt = 0
                t = 0
                if blocks[i] > 0:
                    for j in range(5000):
                        cnt += table[j][i]
                        if cnt == blocks[i]:
                            t = j+1
                            break

                maxtop = max(maxtop, t)
            for i in range(q, q+p):
                table[maxtop][i] = 1
                blocks[i] += 1
            if table[maxtop][0]*table[maxtop][1]*table[maxtop][2]*table[maxtop][3]*table[maxtop][4] == 1:
                del table[maxtop]
                for i in range(5):
                    blocks[i] -= 1

        else:
            maxtop = 0
            cnt = 0
            if blocks[q] > 0:
                t = 0
                for i in range(5000):
                    cnt += table[i][q]
                    if cnt == blocks[q]:
                        t = i + 1
                        break
                maxtop = t

            for i in range(p):
                table[maxtop + i][q] = 1
            blocks[q] += p

            for i in range(p-1, -1, -1):
                k = maxtop + i
                if table[k][0]*table[k][1]*table[k][2]*table[k][3]*table[k][4] == 1:
                    del table[k]
                    for j in range(5):
                        blocks[j] -= 1

    ans = 0
#    for i in range(9,-1,-1):
#        for j in range(5):
#            print(table[i][j], end = "")
#        print("")

    for i in range(5000):
        for j in range(5):
            ans += table[i][j]

    print(ans)

Python: N = input()

while True:
    ls = [[None] * 5] * 5000
    flwr = 0
    for _ in xrange(N):
        d, l, pos = 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 = input()
    # end condition
    if not N:
        break

Python: def solve():
    from bisect import bisect_left
    from sys import stdin
    f_i = stdin
    
    while True:
        n = int(f_i.readline())
        if n == 0:
            break
        
        b = [[0] for i in range(5)]
        for i in range(n):
            d, p, q = map(int, f_i.readline().split())
            q -= 1
            if d == 1:
                if p == 5:
                    continue
                
                h = 0
                for c in b[q:q+p]:
                    if c[-1] > h:
                        h = c[-1]
                
                h += 1
                for c in b[:q] + b[q+p:]:
                    if h not in c:
                        for c in b[q:q+p]:
                            c.append(h)
                        break
                else:
                    for c in b[:q] + b[q+p:]:
                        idx = bisect_left(c, h)
                        c.pop(idx)
                        for j in range(idx, len(c)):
                            c[j] -= 1
            else:
                h = b[q][-1] + 1
                b[q] += list(range(h, h + p))
                diff = 0
                for x in range(h, h + p):
                    x += diff
                    for c in b[:q] + b[q+1:]:
                        if x not in c:
                            break
                    else:
                        for c in b:
                            idx = bisect_left(c, x)
                            c.pop(idx)
                            for j in range(idx, len(c)):
                                c[j] -= 1
                        diff -= 1
        
        print(sum(map(len, b)) - 5)

solve()