Buggy python submission (verdict: Time Limit Exceeded):
MAXN=5000
def deleteBlocks():
    i=MAXN-1
    while i>=0 and set(L[i])!=set([False]):
        if set(L[i])==set([True]):
            del L[i]
            L.insert(0,[False]*5)
        else:
            i-=1

def setBlocks(d,lng,pos):
    if d==1:
        for i in range(MAXN):
            if set(L[i][pos:pos+lng])!=set([False]):
                L[i-1][pos:pos+lng]=[True]*lng
                break
        else:
            L[i][pos:pos+lng]=[True]*lng
    elif d==2:
        for i in range(MAXN):
            if L[i][pos]:
                for j in range(i-lng,i):
                    L[j][pos]=True
                break
        else:
            for j in range(i-lng+1,i+1):
                L[j][pos]=True
    deleteBlocks()

while True:
    n=input()
    if n==0:break
    L=[[False]*5 for i in range(MAXN)]
    for i in range(n):
        d,p,q=map(int,raw_input().split())
        setBlocks(d,p,q-1)
    print sum(sum(i) for i in L)

Fixed submission:
MAXN=5000
def setBlocks(d,lng,pos):
    if d==1:
        for i in range(MAXN):
            if set(L[i][pos:pos+lng])!=set([False]):
                L[i-1][pos:pos+lng]=[True]*lng
                if set(L[i-1])==set([True]):
                    del L[i-1]
                    L.insert(0,[False]*5)
                break
        else:
            L[i][pos:pos+lng]=[True]*lng
            if set(L[i])==set([True]):
                del L[i]
                L.insert(0,[False]*5)
    elif d==2:
        for i in range(MAXN):
            if L[i][pos]:
                for j in range(i-lng,i):
                    L[j][pos]=True
                for j in range(i-lng,i):
                    if set(L[j])==set([True]):
                        del L[j]
                        L.insert(0,[False]*5)
                break
        else:
            for j in range(i-lng+1,i+1):
                L[j][pos]=True
            for j in range(i-lng+1,i+1):
                if set(L[j])==set([True]):
                    del L[j]
                    L.insert(0,[False]*5)

while True:
    n=input()
    if n==0:break
    L=[[False]*5 for i in range(MAXN)]
    for i in range(n):
        d,p,q=map(int,raw_input().split())
        setBlocks(d,p,q-1)
    print sum(sum(i) for i in L)

Buggy python submission (verdict: Time Limit Exceeded):
MAXN=5000
def deleteBlocks():
    i=MAXN-1
    while i>=0 and set(L[i])!=set([False]):
        if set(L[i])==set([True]):
            del L[i]
            L.insert(0,[False]*5)
        else:
            i-=1

def setBlocks(d,lng,pos):
    if d==1:
        for i in range(MAXN):
            if set(L[i][pos:pos+lng])!=set([False]):
                L[i-1][pos:pos+lng]=[True]*lng
                break
        else:
            L[i][pos:pos+lng]=[True]*lng
    elif d==2:
        for i in range(MAXN):
            if L[i][pos]:
                for j in range(i-lng,i):
                    L[j][pos]=True
                break
        else:
            for j in range(i-lng+1,i+1):
                L[j][pos]=True
    deleteBlocks()

while True:
    n=input()
    if n==0:break
    L=[[False]*5 for i in range(MAXN)]
    for i in range(n):
        d,p,q=map(int,raw_input().split())
        setBlocks(d,p,q-1)
    cnt,i=0,MAXN-1
    while i>=0 and set(L[i])!=set([False]):
        cnt+=sum(L[i])
        i-=1
    print cnt

Fixed submission:
MAXN=5000
def setBlocks(d,lng,pos):
    if d==1:
        for i in range(MAXN):
            if set(L[i][pos:pos+lng])!=set([False]):
                L[i-1][pos:pos+lng]=[True]*lng
                if set(L[i-1])==set([True]):
                    del L[i-1]
                    L.insert(0,[False]*5)
                break
        else:
            L[i][pos:pos+lng]=[True]*lng
            if set(L[i])==set([True]):
                del L[i]
                L.insert(0,[False]*5)
    elif d==2:
        for i in range(MAXN):
            if L[i][pos]:
                for j in range(i-lng,i):
                    L[j][pos]=True
                for j in range(i-lng,i):
                    if set(L[j])==set([True]):
                        del L[j]
                        L.insert(0,[False]*5)
                break
        else:
            for j in range(i-lng+1,i+1):
                L[j][pos]=True
            for j in range(i-lng+1,i+1):
                if set(L[j])==set([True]):
                    del L[j]
                    L.insert(0,[False]*5)

while True:
    n=input()
    if n==0:break
    L=[[False]*5 for i in range(MAXN)]
    for i in range(n):
        d,p,q=map(int,raw_input().split())
        setBlocks(d,p,q-1)
    print sum(sum(i) for i in L)