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
}

C++: //14
#include<iostream>
#include<algorithm>

using namespace std;

int main(){
  for(int n;cin>>n,n;){
    const int h=10000;
    bool g[h][5]={{}};
    fill(g[h-1],g[h],true);
    while(n--){
      int d,p,q;
      cin>>d>>p>>q;
      int y;
      for(y=0;;y++){
	if(d==2){
	  if(g[y+1][q-1])break;
	}else{
	  int i;
	  for(i=0;i<p;i++){
	    if(g[y+1][q-1+i])break;
	  }
	  if(i<p)break;
	}
      }
      for(int i=0;i<p;i++){
	int dy=0,dx=0;
	((d==2)?dy:dx)=i;
	g[y-dy][q-1+dx]=true;
      }
      for(int i=0;i<h-1;i++){
	if(count(g[i],g[i+1],true)==5){
	  for(int j=i;j>=1;j--){
	    for(int x=0;x<5;x++){
	      g[j][x]=g[j-1][x];
	    }
	  }
	}
      }
    }
    cout<<count(g[0],g[h-1],true)<<endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

bool field[5000][5];
int top[5];

int main()
{
	int n;
	while (scanf("%d", &n), n)
	{
		memset(field, 0, sizeof(field));
		memset(top, 0, sizeof(top));

		while (n--)
		{
			int d, p, q;
			scanf("%d %d %d", &d, &p, &q);
			--q;
			if (d == 1)
			{
				int maxTop = -1;
				for (int i = 0; i < p; ++i)
					maxTop = max(maxTop, top[q + i]);
				for (int i = 0; i < p; ++i)
					field[maxTop][q + i] = true;
				for (int i = 0; i < p; ++i)
					top[q + i] = maxTop + 1;
			}
			else
			{
				for (int i = 0; i < p; ++i)
					field[top[q] + i][q] = true;
				top[q] += p;
			}
		}
		int sum = 0;
		for (int i = 0; i < 5000; ++i)
		{
			int s = 0;
			for (int j = 0; j < 5; ++j)
				if (field[i][j])
					++s;
			if (s != 5)
				sum += s;
		}
		printf("%d\n", sum);
	}

	return 0;
}