n = int(input()) n = n // 3 * 3 if n < 100: ans = 0 for i in range(1, 10): for j in range(0, 10): if 10 * i + j <= n and (i + j) % 3 == 0: ans += 1 print(ans) else: s = list(map(int, str(n))) d = len(s) for i in range(d): if s[i] % 3: s[i] = s[i] // 3 for j in range(i + 1, d): s[j] = 0 if s[0] == 0: s[1] = 1 break else: s[i] //= 3 ans = 15 for i in range(d): ans += s[i] * 4**(d - 1 - i) print(ans)