l = [15, 24, 42, 51, 18, 27, 45, 54, 72, 81, 12, 21, 84, 75, 57, 48, 87, 78] n = int(input()) s = [0] st = set() while s: p = s.pop() if p in st or p > n: continue l.append(p) st.add(p) s.append(p * 10) s.append(p * 10 + 3) s.append(p * 10 + 6) s.append(p * 10 + 9) ans = 0 for x in l: if 10 <= x <= n: ans += 1 print(ans)