import itertools
from functools import lru_cache

@lru_cache(maxsize=1000)
def f(k, t='357'):
    tmp = [int(i) for i in t]
    res = []
    if k ==len(tmp):
        return list((set([i for i in itertools.permutations(tmp, k)])))
    else:
        a1 = ""
        for i in sorted(t+'3'):
            a1+=i
        a2 = ""
        for i in sorted(t+'5'):
            a2+=i
        a3 = ""
        for i in sorted(t+'7'):
            a3+=i
        return list(set(f(k, a1) + f(k, a2) + f(k, a3)))

def f2(res, N):
    cnt = 0
    n  = int(N)
    for tmp in res:
        s = ''
        for i in tmp:
            s += str(i)
        if int(s) <= n:
            cnt += 1
    return cnt
def f3(N):
    keta = len(N)
    result = 0
    for i in range(3, keta+1):
        res = f(i)
        result += f2(res, N)
    return result
if __name__ == '__main__':
    print(f3("99999999"))