import math n = int(input()) d = int(math.log10(n)) + 1 digits = [int(c) for c in str(n)] leading_zero = [0, 0, 30] + [3*4**i for i in range(2, 10)] if n < 100: cnt = 0 for i in range(12, n+1, 3): cnt += 1 print(cnt) else: dp = [[0]*2 for i in range(d+1)] GREATEST = 0 LESS = 1 dp[0][GREATEST] = 1 if digits[0] % 3 == 0 else 0 dp[0][LESS] = ((digits[0]-1)//3) for i in range(0, d): dp[i+1][GREATEST] = dp[i][GREATEST] * (1 if digits[i] % 3 == 0 else 0) dp[i+1][LESS] = dp[i][GREATEST] * ((digits[i]-1)//3 + 1) dp[i+1][LESS] = dp[i][LESS] * (digits[i]//3 + 1) print(sum(dp[d]) + sum(leading_zero[:d]))