import sys from collections import defaultdict, Counter from itertools import product, groupby, count, permutations, combinations from math import pi, sqrt, ceil, floor from collections import deque from bisect import bisect, bisect_left, bisect_right from string import ascii_lowercase from functools import lru_cache, reduce INF = float("inf") sys.setrecursionlimit(10 ** 7) # 4近傍(右, 下, 左, 上) dy = [0, -1, 0, 1] dx = [1, 0, -1, 0] def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W S = "" memo = {} def dfs(idx, tight, find, rest): if idx >= len(S): return find or rest == 0 if (idx, tight, find, rest) in memo: return memo[(idx, tight, find, rest)] x = int(S[idx]) upper = x if tight else 9 ans = 0 for i in range(upper + 1): ans += dfs(idx + 1, tight and (i == x), find or i == 3, (rest + i) % 3) memo[(idx, tight, find, rest)] = ans return ans def main(): global S S = str(10 ** int(input())) print(dfs(0, True, False, 0) - 1) if __name__ == '__main__': main()