def f(n, k):
  num_bit = 3 - bin(k).count('1')
  c = [0, 0, 1, 3]
  return 3**n - num_bit*2**n + c[num_bit]

# k = 0:(), 1:(3), 2(5), 3:(3, 5), 4:(7), 5:(3, 7), 6:(5, 7), 7:(3, 5, 7)
def solve(N, k=0):
  n = len(N)
  num_used = bin(k).count('1')
  if n==0 or n+num_used<3:
    return 0
  else:
    a = int(N[0])
    base = sum([f(i, 0) for i in range(3, n)])
    if a > 7:
      return sum([f(i, 0) for i in range(3, n+1)])
    elif a == 7:
      return base + f(n-1, k|2) + f(n-1, k|1) + solve(N[1:], k|4)
    elif a > 5:
      return base + f(n-1, k|2) + f(n-1, k|1)
    elif a == 5:
      return base + f(n-1, k|1) + solve(N[1:], k|2)
    elif a > 3:
      return base + f(n-1, k|1)
    elif a == 3:
      return base + solve(N[1:], k|1)
    elif a > 0:
      return base
    else: # a == 0
      return solve(N[1:])

print(solve(input()))