Buggy python submission (verdict: Runtime Error):
while 1:
	n=input()
	if n==0:break
	hmax=1000
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	for d,p,q in block:
		if d==1:
			for li in range(hmax-1,-1,-1):
				if field[li][q-1:q+p-1]!=[0]*p:
					field[li+1][q-1:q+p-1]=[1]*p
					break
			else:
				field[0][q-1:q+p-1]=[1]*p
		else:
			for li in range(hmax-1,-1,-1):
				if field[li][q-1]!=0:
					for i in range(p):
						field[li+i+1][q-1]=1
					break
			else:
				for i in range(p):
					field[li+i][q-1]=1
		i=0
		while 1:
			if field[i]==[1]*5:
				del field[i]
				hmax-=1
			elif field[i]==[0]*5:
				break
			else:
				i+=1
	print sum([sum(field[li]) for li in range(hmax)])

Fixed submission:
while 1:
	n=input()
	if n==0:break
	hmax=7500
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	top=[0]*5
	for d,p,q in block:
		if d==1:
			h=max(top[q-1:q-1+p])
			field[h][q-1:q-1+p]=[1]*p
			top[q-1:q-1+p]=[max(top)+1]*p
		else:
			h=top[q-1]
			for i in range(p):
				field[h+i][q-1]=1
			top[q-1]=h+p
		h=0
		while 1:
			if field[h]==[1]*5:
				del field[h]
				top=[x-1 for x in top]
			elif field[h]==[0]*5:
				break
			else:
				h+=1
		for w in range(5):
			for h in range(top[w]-1,-2,-1):
				if field[h][w]==1 or h==-1:
					top[w]=h+1
					break
	print sum([sum(field[h]) for h in range(max(top))])

Buggy python submission (verdict: Time Limit Exceeded):
while 1:
	n=input()
	if n==0:break
	hmax=10000
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	for d,p,q in block:
		if d==1:
			for li in range(hmax-1,-1,-1):
				if field[li][q-1:q+p-1]!=[0]*p:
					field[li+1][q-1:q+p-1]=[1]*p
					break
			else:
				field[0][q-1:q+p-1]=[1]*p
		else:
			for li in range(hmax-1,-1,-1):
				if field[li][q-1]!=0:
					for i in range(p):
						field[li+i+1][q-1]=1
					break
			else:
				for i in range(p):
					field[li+i][q-1]=1
		i=0
		while 1:
			if field[i]==[1]*5:
				del field[i]
				hmax-=1
			elif field[i]==[0]*5:
				break
			else:
				i+=1
	print sum([sum(field[li]) for li in range(hmax)])

Fixed submission:
while 1:
	n=input()
	if n==0:break
	hmax=7500
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	top=[0]*5
	for d,p,q in block:
		if d==1:
			h=max(top[q-1:q-1+p])
			field[h][q-1:q-1+p]=[1]*p
			top[q-1:q-1+p]=[max(top)+1]*p
		else:
			h=top[q-1]
			for i in range(p):
				field[h+i][q-1]=1
			top[q-1]=h+p
		h=0
		while 1:
			if field[h]==[1]*5:
				del field[h]
				top=[x-1 for x in top]
			elif field[h]==[0]*5:
				break
			else:
				h+=1
		for w in range(5):
			for h in range(top[w]-1,-2,-1):
				if field[h][w]==1 or h==-1:
					top[w]=h+1
					break
	print sum([sum(field[h]) for h in range(max(top))])

Buggy python submission (verdict: Runtime Error):
while 1:
	n=input()
	if n==0:break
	hmax=10000
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	h=0
	for d,p,q in block:
		if d==1:
			for li in range(h+1,-2,-1):
				if field[li][q-1:q+p-1]!=[0]*p or li==-1:
					field[li+1][q-1:q+p-1]=[1]*p
					h=max(h,li+1)
					break
		else:
			for li in range(h+1,-2,-1):
				if field[li][q-1]!=0 or li==-1:
					for i in range(p):
						field[li+i+1][q-1]=1
					h=max(h,li+1+q)
					break
		i=0
		while 1:
			if field[i]==[1]*5:
				del field[i]
				h-=1
			elif field[i]==[0]*5:
				break
			else:
				i+=1
	print sum([sum(field[li]) for li in range(hmax)])

Fixed submission:
while 1:
	n=input()
	if n==0:break
	hmax=7500
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	top=[0]*5
	for d,p,q in block:
		if d==1:
			h=max(top[q-1:q-1+p])
			field[h][q-1:q-1+p]=[1]*p
			top[q-1:q-1+p]=[max(top)+1]*p
		else:
			h=top[q-1]
			for i in range(p):
				field[h+i][q-1]=1
			top[q-1]=h+p
		h=0
		while 1:
			if field[h]==[1]*5:
				del field[h]
				top=[x-1 for x in top]
			elif field[h]==[0]*5:
				break
			else:
				h+=1
		for w in range(5):
			for h in range(top[w]-1,-2,-1):
				if field[h][w]==1 or h==-1:
					top[w]=h+1
					break
	print sum([sum(field[h]) for h in range(max(top))])

Buggy python submission (verdict: Runtime Error):
while 1:
	n=input()
	if n==0:break
	hmax=5000
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	h=0
	for d,p,q in block:
		if d==1:
			for li in range(h,-2,-1):
				if field[li][q-1:q+p-1]!=[0]*p or li==-1:
					field[li+1][q-1:q+p-1]=[1]*p
					h=max(h,li+2)
					break
		else:
			for li in range(h,-2,-1):
				if field[li][q-1]!=0 or li==-1:
					for i in range(p):
						field[li+i+1][q-1]=1
					h=max(h,li+1+p)
					break
		i=0
		while 1:
			if field[i]==[1]*5:
				del field[i]
				h-=1
			elif field[i]==[0]*5:
				break
			else:
				i+=1
	print sum([sum(field[li]) for li in range(h)])

Fixed submission:
while 1:
	n=input()
	if n==0:break
	hmax=7500
	block=[map(int,raw_input().split()) for i in range(n)]
	field=[[0]*5 for i in range(hmax)]
	top=[0]*5
	for d,p,q in block:
		if d==1:
			h=max(top[q-1:q-1+p])
			field[h][q-1:q-1+p]=[1]*p
			top[q-1:q-1+p]=[max(top)+1]*p
		else:
			h=top[q-1]
			for i in range(p):
				field[h+i][q-1]=1
			top[q-1]=h+p
		h=0
		while 1:
			if field[h]==[1]*5:
				del field[h]
				top=[x-1 for x in top]
			elif field[h]==[0]*5:
				break
			else:
				h+=1
		for w in range(5):
			for h in range(top[w]-1,-2,-1):
				if field[h][w]==1 or h==-1:
					top[w]=h+1
					break
	print sum([sum(field[h]) for h in range(max(top))])