Buggy python submission (verdict: Time Limit Exceeded):
ans=0
n=int(input())
def tri(x):
	p=""
	while x>0:
		p+=str(x%4)
		x//=4
	return p[::-1]
if n>=50000000:
	ans=4470
	k=51111113
	i=32768
else:
	i=16
	k=357
while k<=n:
	c0=0
	c1=0
	c2=0
	for j in range(len(tri(i))):
		if tri(i)[j]=="1":
			c0+=1
		elif tri(i)[j]=="2":
			c1+=1
		elif tri(i)[j]=="3":
			c2+=1
		else:
			c0=0
			break
	if c0!=0 and c1!=0 and c2!=0:
		ans+=1
	i+=1
	k=0
	for j in range(len(tri(i))):
		k+=(int(tri(i)[-j-1])*2+1)*(10**j)
print(ans)

Fixed submission:
n=input()
p=""
for i in range(len(n)):
	if int(n[i])>7:
		p+="3"*(len(n)-i)
		break
	elif int(n[i])==7:
		p+="3"
	elif int(n[i])==6:
		p+="2"
		p+="3"*(len(n)-i-1)
		break
	elif int(n[i])==5:
		p+="2"
	elif int(n[i])==4:
		p+="1"
		p+="3"*(len(n)-i-1)
		break
	elif int(n[i])==3:
		p+="1"
	else:
		p+="0"
total=0
for i in range(len(n)):
	total+=int(p[-i-1])*(4**i)
def tet(x):
	q=""
	while x!=0:
		q+=str(x%4)
		x//=4
	if len(q)==0:
		q="0"
	return q[::-1]
ans=[]
cnt=0
for i in range(total):
	if tet(i+1).count("0")==0:
		ans.append(tet(i+1))
for i in range(len(ans)):
	if ans[i].count("1") and ans[i].count("2")\
	and ans[i].count("3"):
		cnt+=1
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
ans=0
n=int(input())
cnt=0
def tri(x):
	p=""
	while x>0:
		p+=str(x%4)
		x//=4
	return p[::-1]
i=16
k=357
while k<=n:
	c0=0
	c1=0
	c2=0
	for j in range(len(tri(i))):
		if tri(i)[j]=="1":
			c0+=1
		elif tri(i)[j]=="2":
			c1+=1
		elif tri(i)[j]=="3":
			c2+=1
		else:
			c0=0
			break
	if c0!=0 and c1!=0 and c2!=0:
		ans+=1
	i+=1
	k=0
	for j in range(len(tri(i))):
		k+=(int(tri(i)[-j-1])*2+1)*(10**j)
print(ans)

Fixed submission:
n=input()
p=""
for i in range(len(n)):
	if int(n[i])>7:
		p+="3"*(len(n)-i)
		break
	elif int(n[i])==7:
		p+="3"
	elif int(n[i])==6:
		p+="2"
		p+="3"*(len(n)-i-1)
		break
	elif int(n[i])==5:
		p+="2"
	elif int(n[i])==4:
		p+="1"
		p+="3"*(len(n)-i-1)
		break
	elif int(n[i])==3:
		p+="1"
	else:
		p+="0"
total=0
for i in range(len(n)):
	total+=int(p[-i-1])*(4**i)
def tet(x):
	q=""
	while x!=0:
		q+=str(x%4)
		x//=4
	if len(q)==0:
		q="0"
	return q[::-1]
ans=[]
cnt=0
for i in range(total):
	if tet(i+1).count("0")==0:
		ans.append(tet(i+1))
for i in range(len(ans)):
	if ans[i].count("1") and ans[i].count("2")\
	and ans[i].count("3"):
		cnt+=1
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
ans=0
n=int(input())
cnt=0
def tri(x):
	p=""
	while x>0:
		p+=str(x%4)
		x//=4
	return p[::-1]
i=16
k=357
while k<=n:
	c0=0
	c1=0
	c2=0
	for j in range(len(tri(i))):
		if tri(i)[j]=="1":
			c0+=1
		elif tri(i)[j]=="2":
			c1+=1
		elif tri(i)[j]=="3":
			c2+=1
		else:
			c0=0
			break
	if c0!=0 and c1!=0 and c2!=0:
		ans+=1
	i+=1
	k=0
	for j in range(len(tri(i))):
		k+=(int(tri(i)[-j-1])*2+1)*(10**j)
print(ans)

Fixed submission:
n=input()
p=""
for i in range(len(n)):
	if int(n[i])>7:
		p+="3"*(len(n)-i)
		break
	elif int(n[i])==7:
		p+="3"
	elif int(n[i])==6:
		p+="2"
		p+="3"*(len(n)-i-1)
		break
	elif int(n[i])==5:
		p+="2"
	elif int(n[i])==4:
		p+="1"
		p+="3"*(len(n)-i-1)
		break
	elif int(n[i])==3:
		p+="1"
	else:
		p+="0"
total=0
for i in range(len(n)):
	total+=int(p[-i-1])*(4**i)
def tet(x):
	q=""
	while x!=0:
		q+=str(x%4)
		x//=4
	if len(q)==0:
		q="0"
	return q[::-1]
ans=[]
cnt=0
for i in range(total):
	if tet(i+1).count("0")==0:
		ans.append(tet(i+1))
for i in range(len(ans)):
	if ans[i].count("1") and ans[i].count("2")\
	and ans[i].count("3"):
		cnt+=1
print(cnt)