n = int(input()) if n < 10: print(0) elif n < 100: print(n // 3 - 3) else: ans = 30 s = str(n) d = len(s) for i in range(3, d): ans += 3 * pow(4, i - 1) for i in range(d): q, r = divmod(int(s[i]), 3) if r != 0: ans += q * pow(q + 1, d - 1) else: ans += (q - 1) * pow(q, d - 1) if i == 0: ans += 1 print(ans)