from bisect import bisect_right import itertools n = int(input()) ans = [i * 3 for i in range(4, 34)] for lst in itertools.product("0369", repeat = 9): num = int("".join(lst)) if num // 100 == 0: continue ans.append(num) ans.sort() print(bisect_right(ans, n))