from collections import deque n = int(input()) if n < 100: ans = 0 for i in range(10,n+1): if i%3 == 0: if (int(str(i)[0])+int(str(i)[1]))%3 == 0: ans += 1 print(ans) exit() else: ans = 30 q = deque([]) for i in range(10,100): if i%3 == 0: if int(str(i)[0])%3 == 0 and int(str(i)[1])%3 == 0: q.append(str(i)) while q: now = q.popleft() if int(now) > n: continue if int(now) >= 100: ans += 1 q.append(now+"3") q.append(now+"6") q.append(now+"9") q.append(now+"0") print(ans)