n = int(input()) X1 = [1, 4, 7] X2 = [2, 5, 8] X0 = [0, 3, 6, 9] X = set() for a in X1: for b in X2: X.add(10*a+b) X.add(10*b+a) import itertools X0 = [str(c) for c in X0] for p in itertools.product(X0, repeat=10): x = ''.join(p) x = int(x) if 0 <= x <= 9: continue X.add(x) X = list(X) X.sort() import bisect ans = bisect.bisect_right(X, n) print(ans)