from itertools import product



N = int(eval(input()))



S = str(N)

n = len(S)



ans = 0

for i in range(3, n + 1):

    for p in product(['3', '5', '7'], repeat=i):

        x = int(''.join(p))

        if x <= N and len(set(p) & {'3', '5', '7'}) == 3:

            ans += 1

print(ans)

from itertools import product



N = int(eval(input()))



S = [int(x) for x in str(N)]

n = len(S)



# dp[pos][smaller?][started?][3?][5?][7?]

dp = [[[[[[0] * 2 for _ in range(2)] for _ in range(2)]

        for _ in range(2)] for _ in range(2)] for _ in range(n + 1)]

dp[0][0][0][0][0][0] = 1



for i, j, k, c3, c5, c7 in product(list(range(n)), [0, 1], [0, 1], [0, 1], [0, 1],

                                   [0, 1]):

    num = [0, 3, 5, 7]

    if not j:

        num = [x for x in num if x <= S[i]]

    if k:

        num = [x for x in num if x != 0]

    for x in num:

        f0 = k or x != 0

        f3 = c3 or x == 3

        f5 = c5 or x == 5

        f7 = c7 or x == 7

        dp[i + 1][j or x < S[i]][f0][f3][f5][f7] += dp[i][j][k][c3][c5][c7]



ans = sum([dp[n][j][1][1][1][1] for j in [0, 1]])

print(ans)

from itertools import product

N = int(eval(input()))
S = str(N)
n = len(S)
ans = 0
for i in range(3, n + 1):
    for p in product(["3", "5", "7"], repeat=i):
        x = int("".join(p))
        if x <= N and len(set(p) & {"3", "5", "7"}) == 3:
            ans += 1
print(ans)

from itertools import product

N = int(eval(input()))
S = [int(x) for x in str(N)]
n = len(S)
# dp[pos][smaller?][started?][3?][5?][7?]
dp = [
    [
        [[[[0] * 2 for _ in range(2)] for _ in range(2)] for _ in range(2)]
        for _ in range(2)
    ]
    for _ in range(n + 1)
]
dp[0][0][0][0][0][0] = 1
for i, j, k, c3, c5, c7 in product(
    list(range(n)), [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]
):
    num = [0, 3, 5, 7]
    if not j:
        num = [x for x in num if x <= S[i]]
    if k:
        num = [x for x in num if x != 0]
    for x in num:
        f0 = k or x != 0
        f3 = c3 or x == 3
        f5 = c5 or x == 5
        f7 = c7 or x == 7
        dp[i + 1][j or x < S[i]][f0][f3][f5][f7] += dp[i][j][k][c3][c5][c7]
ans = sum([dp[n][j][1][1][1][1] for j in [0, 1]])
print(ans)

-S = str(N) +S = [int(x) for x in str(N)] -ans = 0 -for i in range(3, n + 1): -    for p in product(["3", "5", "7"], repeat=i): -        x = int("".join(p)) -        if x <= N and len(set(p) & {"3", "5", "7"}) == 3: -            ans += 1 +# dp[pos][smaller?][started?][3?][5?][7?] +dp = [ +    [ +        [[[[0] * 2 for _ in range(2)] for _ in range(2)] for _ in range(2)] +        for _ in range(2) +    ] +    for _ in range(n + 1) +] +dp[0][0][0][0][0][0] = 1 +for i, j, k, c3, c5, c7 in product( +    list(range(n)), [0, 1], [0, 1], [0, 1], [0, 1], [0, 1] +): +    num = [0, 3, 5, 7] +    if not j: +        num = [x for x in num if x <= S[i]] +    if k: +        num = [x for x in num if x != 0] +    for x in num: +        f0 = k or x != 0 +        f3 = c3 or x == 3 +        f5 = c5 or x == 5 +        f7 = c7 or x == 7 +        dp[i + 1][j or x < S[i]][f0][f3][f5][f7] += dp[i][j][k][c3][c5][c7] +ans = sum([dp[n][j][1][1][1][1] for j in [0, 1]])