from collections import deque N = int(input()) l = [12, 15, 18, 21, 24, 27, 42, 45, 48, 51, 54, 57, 72, 75, 78, 81, 84, 87] ans = -3 q = deque([3,6,9]) while len(q) > 0: num = q.popleft() if num <= N: ans += 1 for y in [0, 3, 6, 9]: q.append(num*10 + y) for i in l: if i <= N: ans += 1 print(ans)