#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v.begin(), v.end())
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }



string N;
long long dp[21][10010][3][3]; // digit, last4, clear, smaller

bool judge(int n) {
  if ( (n/100) != 51) return false;
  if (n % 10 != 3) return false;
  return true;
}

int main() {
  while (cin >> N) {
	memset(dp, 0, sizeof(dp));
	dp[0][0][0][0] = 1;
	for (int i = 0; i <= N.size(); ++i) {
	  for (int j = 0; j <= 9999; ++j) {
		for (int k = 0; k < 10; ++k) {
		  int nj = (j % 1000) * 10 + k;

		  // smaller -> smaller
		  {
			// clear -> clear
			dp[i+1][nj][1][1] += dp[i][j][1][1];

			// nonclear -> 
			if (judge(nj)) dp[i+1][nj][1][1] += dp[i][j][0][1];
			else dp[i+1][nj][0][1] += dp[i][j][0][1];
		  }

		  // same -> smaller
		  if (k < (int)(N[i] - '0')) {
			// clear -> clear
			dp[i+1][nj][1][1] += dp[i][j][1][0];

			// nonclear ->
			if (judge(nj)) dp[i+1][nj][1][1] += dp[i][j][0][0];
			else dp[i+1][nj][0][1] += dp[i][j][0][0];
		  }

		  // same -> same
		  if (k == (int)(N[i] - '0')) {
			// clear -> clear
			dp[i+1][nj][1][0] += dp[i][j][1][0];

			// nonclear ->
			if (judge(nj)) dp[i+1][nj][1][0] += dp[i][j][0][0];
			else dp[i+1][nj][0][0] += dp[i][j][0][0];
		  }
		  
		}
	  }
	}

	long long res = 0;
	for (int j = 0; j <= 9999; ++j) {
	  for (int smaller = 0; smaller < 2; ++smaller) {
		res += dp[N.size()][j][1][smaller];
	  }
	}

	cout << res << endl;
  }
}



























