n=input()
h=0
k=0
if int(n[0])>=7:
  for i in range(1,len(n)+1):
    h+=3**i-2**i*3+3
    
elif int(n[0])>=5:
  for i in range(1,len(n)+1):
    h+=3**i-2**i*3+3
    k-=3**(len(n)-1)-2**(len(n)+1)
elif int(n[0])>=3:
  for i in range(1,len(n)+1):
    h+=3**i-2**i*3+3
    k-=2*(3**(len(n)-1)-2**(len(n)+1))
  
else:
  for i in range(1,len(n)):
    h+=3**i-2**i*3+3
    
       
print(h-k)