// 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

int dp[20][5][2][10];

void print_debug(int a, int b, int c, int x, int y, int z, int val) {
    /*
    if(dp[x][y][z]) {
        printf("move: %lld, %lld, %lld <- %lld, %lld, %lld (value = %lld, digit = %lld)\n", a, b, c, x, y, z, dp[x][y][z], val);
    }
    */
}
 
signed main() {
    int init_val; cin >> init_val;
    string s = to_string(init_val);
    int N = s.length();

    dp[0][0][0][0] = 1;
    string pat = "51?3";
    for(int i=0; i<N; i++) {
        for(int k=0; k<4; k++) {
            for(int f=0; f<2; f++) {
                for(int y=0; y<10; y++) {
                    int lim = f ? 9 : s[i] - '0';
                    for(int x=0; x<lim+1; x++) {
                        bool is_match = (pat[k] == '?' || pat[k] - '0' == x);
                        if(is_match) {
                            if(k+1 != 4) {
                                dp[i+1][k+1][f || x<lim][x] += dp[i][k][f][y];
                                // print_debug(i+1, k+1, f||x<lim, i, k, f, x);
                            }
                        }
                        else {
                            int nidx;
                            if(k == 3 && y == 5 && x == 1) nidx = 2;
                            else nidx = (x == 5);
                            dp[i+1][nidx][f || x < lim][x] += dp[i][k][f][y];
                            // print_debug(i+1, 0, f||x<lim, i, k, f, x);
                        }
                    }
                }
            }
        }
    }

    int sum = -1;
    for(int i=0; i<4; i++) {
        for(int j=0; j<2; j++) {
            for(int k=0; k<10; k++) {
                sum += dp[N][i][j][k];
            }
            // printf("dp[%lld][%lld][%lld] = %lld\n", N, i, j, dp[N][i][j]);
        }
    }
    cout << init_val - sum << endl;
    return 0;
}
