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

    # end condition
    if not N:
        break

Python: while 1:
    N = int(input())
    if N == 0:
        break
    L = 5000
    MP = [[0]*L for i in range(5)]
    cs = [L]*5
    us = []
    U = [0]*L
    for i in range(N):
        d, p, q = map(int, input().split()); q -= 1
        if d == 1:
            y = L
            for j in range(p):
                y = min(y, cs[q+j])
            y -= 1
            for j in range(p):
                MP[q+j][y] = 1
                cs[q+j] = y

            if all(MP[j][y] for j in range(5)):
                us.append(y)
        else:
            y = cs[q] - p
            for j in range(p):
                MP[q][y+j] = 1
            cs[q] = y
            for j in range(p):
                if all(MP[k][y+j] for k in range(5)):
                    us.append(y+j)
        if us:
            y0 = us[-1]
            for y in us:
                U[y] = 1
            for j in range(5):
                cur = y0
                for k in range(y0, cs[j]-1, -1):
                    if U[k]:
                        continue
                    MP[j][cur] = MP[j][k]
                    cur -= 1
                for k in range(cur, cs[j]-1, -1):
                    MP[j][k] = 0
                while cur < L and MP[j][cur] == 0:
                    cur += 1
                cs[j] = cur
            for y in us:
                U[y] = 0
            us = []
    ans = 0
    for j in range(5):
        for k in range(cs[j], L):
            ans += MP[j][k]
    print(ans)

Python: end = [1,1,1,1,1]
while True:
  n = int(input())
  if n == 0:
    break
  mp = [[0] * 5 for _ in range(4000)]
  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: #! /usr/bin/python
# -*- coding: utf-8 -*-

YOKO = 1
TATE = 2

def main():
    # input
    while True:
        N = int(raw_input())
        if N == 0:
            break

        data = []
        for x in xrange(N):
            data.append(map(int, raw_input().split()))

        print(restBlock(data))

def restBlock(data):
    state = []
    for x in data:
        if x[0] == YOKO:
            block = makeYokoBlock(x)
            state = yokoBlock(state, block)
        else:
            blocks = makeTateBlocks(x)
            state = tateBlock(state, blocks)

        state = clearTetoris(state)
    return countRestBlock(state)

def clearTetoris(state):
    return filter(lambda x:sum(x) < 5, state)

# Blockが重なっていたらFalse
def checkBlock(a, b):
    if max([sum(x) for x in zip(a, b)]) > 1:
        return False
    else:
        return True

def countRestBlock(state):
    return sum([sum(x) for x in state])

def tateBlock(state, blocks):
    for x in blocks:
        state = yokoBlock(state, x)
    return state

def makeTateBlocks(block_data):
    sckellton = [0, 0, 0, 0, 0]
    i = block_data[2] - 1
    sckellton[i] = 1
    return [sckellton for x in xrange(block_data[1])]

def yokoBlock(state, block):

    if len(state) == 0:
        state.append(block)
        return state

    for i, x in enumerate(state[::-1]):
        if checkBlock(x, block):
            pass
        else:
            if i == 0:
                state.append(block)
            else:
                state[-i] = [sum(x) for x in zip(state[-i], block)]
            break
    else:
        state[0] = [sum(x) for x in zip(state[0], block)]

    return state

def makeYokoBlock(block_data):
    block = [0, 0, 0, 0, 0]
    i = block_data[2]
    j = block_data[1]
    for x in xrange(j):
        block[i - 1 + x] = 1

    return block

def test():
    data = [[1,4,1],
            [1,3,1],
            [2,2,4],
            [2,3,5]]

    # print(makeYokoBlock([1,3,2]))
    # print(checkBlock([1, 1, 0, 0, 0], [0, 0, 1, 1, 0]))
    # state = [[1, 1, 1, 1, 0], [1, 1, 0, 0, 0], [1, 1, 0, 0, 0]]
    # print(yokoBlock(state, [1, 0, 1, 0, 1]))
    # state = []
    # print(yokoBlock(state, [1, 0, 1, 0, 1]))
    # state = [[1, 1, 1, 1, 0]]
    # print(yokoBlock(state, [1, 0, 1, 0, 1]))
    # state = [[1, 1, 1, 1, 1], [1, 1, 0, 0, 0], [1, 1, 1, 1, 1]]
    # print(clearTetoris(state))
    # print(makeTateBlocks([2, 3, 2]))
    # state = [[1, 1, 0, 1, 1], [1, 1, 0, 0, 0], [1, 1, 0, 1, 1]]
    # print(tateBlock(state, [[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]]))
    # print(countRestBlock(state))
    # print(restBlock(data))

if __name__=='__main__':
    # test()
    main()