import sys if len(sys.argv) == 2: sys.stdin = open(sys.argv[1]) n = int(input()) if n < 10: print(0) elif n < 100: print(n // 3 - 3) else: ans = 30 - 12 s = str(n) d = len(s) C = [0 for _ in range(10)] C[0] = 1 for i in range(1, 10): C[i] = C[i - 1] * 4 ok = True for i in range(d): num = int(s[i]) for c in range(0, num, 3): ans += C[d - i - 1] if num % 3 != 0: ok = False break if ok: ans += 1 print(ans - 4)