#include<bits/stdc++.h>
using namespace std;
#define M 11

int n, a[4], dp[M][M][M][M][4];

string change(int n){
    if (n == 0) return "0";
    string s = "";
    while (n > 0){
        s = (char)(n%10 + '0') + s;
        n /= 10;
    }
    return s;
}

int cal(int n){
    string s = change(n);
    int len = s.size();
    for (int x = 0; x < 3; x++){
        if (a[x] < s[0] - '0'){
            if (a[x] == 3) dp[1][1][0][0][0]++;
            if (a[x] == 5) dp[1][0][1][0][0]++;
            if (a[x] == 7) dp[1][0][0][1][0]++;
        }
        else{
            if (a[x] > s[0] - '0'){
                if (a[x] == 3) dp[1][1][0][0][2]++;
                if (a[x] == 5) dp[1][0][1][0][2]++;
                if (a[x] == 7) dp[1][0][0][1][2]++;
            }
            else{
                if (a[x] == 3) dp[1][1][0][0][1]++;
                if (a[x] == 5) dp[1][0][1][0][1]++;
                if (a[x] == 7) dp[1][0][0][1][1]++;
            }
        }
    }
    for (int i = 1; i < len; i++){
        for (int thr = 0; thr < 11; thr++){
            for (int fiv = 0; fiv < 11; fiv++){
                for (int sev = 0; sev < 11; sev++){
                    for (int sta = 0; sta < 3; sta++){
                        if (dp[i][thr][fiv][sev][sta] == 0) continue;
                        int cur = dp[i][thr][fiv][sev][sta];
                        for (int x = 0; x < 3; x++){
                            if (sta == 0 || sta == 2){
                                if (x == 0) dp[i+1][thr+1][fiv][sev][sta] += cur;
                                if (x == 1) dp[i+1][thr][fiv+1][sev][sta] += cur;
                                if (x == 2) dp[i+1][thr][fiv][sev+1][sta] += cur;
                            }
                            else{
                                if (a[x] < s[i] - '0'){
                                    if (x == 0) dp[i+1][thr+1][fiv][sev][0] += cur;
                                    if (x == 1) dp[i+1][thr][fiv+1][sev][0] += cur;
                                    if (x == 2) dp[i+1][thr][fiv][sev+1][0] += cur;
                                }
                                else{
                                    if (a[x] > s[i] - '0'){
                                        if (x == 0) dp[i+1][thr+1][fiv][sev][2] += cur;
                                        if (x == 1) dp[i+1][thr][fiv+1][sev][2] += cur;
                                        if (x == 2) dp[i+1][thr][fiv][sev+1][2] += cur;
                                    }
                                    else{
                                        if (x == 0) dp[i+1][thr+1][fiv][sev][1] += cur;
                                        if (x == 1) dp[i+1][thr][fiv+1][sev][1] += cur;
                                        if (x == 2) dp[i+1][thr][fiv][sev+1][1] += cur;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    int res = 0;
    for (int i = 1; i <= len; i++){
        for (int thr = 1; thr < 11; thr++){
            for (int fiv = 1; fiv < 11; fiv++){
                for (int sev = 1; sev < 11; sev++){
                    for (int sta = 0; sta < (i == len ? 2 : 3); sta++){
                        res += dp[i][thr][fiv][sev][sta];
                    }
                }
            }
        }
    }
    return res;
}

int main(){
    a[0] = 3, a[1] = 5, a[2] = 7;
    int n;
    cin >> n;
    cout << cal(n);
    return 0;
}

Measured memory_bytes: 384