Problem:
Score : 300 points

Problem Statement

You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally "Seven-Five-Three numbers") are there?

Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:

When the number is written in base ten, each of the digits 7, 5 and 3 appears at least once, and the other digits never appear.

Constraints

1 \leq N < 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go-San numbers between 1 and N (inclusive).

Sample Input 1

575

Sample Output 1

4

There are four Shichi-Go-San numbers not greater than 575: 357, 375, 537 and 573.

Sample Input 2

3600

Sample Output 2

13

There are 13 Shichi-Go-San numbers not greater than 3600: the above four numbers, 735, 753, 3357, 3375, 3537, 3557, 3573, 3575 and 3577.

Sample Input 3

999999999

Sample Output 3

26484

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<sstream>
using namespace std;
typedef  long long int lint;
typedef pair<int, int> P;
typedef pair<lint, lint> LLP;
typedef pair<char, char>CP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v,hoge) upper_bound(v.begin(),v.end(),hoge)
#define lower(v,hoge) lower_bound(v.begin(),v.end(),hoge)
#define llower(v,hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v,hoge) *upper_bound(v.begin(), v.end(), hoge)

const lint MOD = pow(10, 9) + 7;

lint gcd(lint a, lint b) {
	//aとbの最大公約数を返す
	if (a < b)swap(a, b);
	if (a % b == 0)return b;
	return gcd(a % b, b);
}

lint lcm(lint a, lint b) {
	//aとbの最小公倍数を返す
	return a / gcd(a, b) * b;
}

lint sgm(lint n) {
	//0からの総和を返す
	return n * (n + 1) / 2;
}

lint fact(lint n) {
	//n!を返す
	if (n == 0) return 1;
	else return n * fact(n - 1);
}

lint power(lint x, lint n, lint m) {
	//(x^n)%mを返す
	lint res = 1;
	x %= m;
	while (n > 0) {
		if (n % 2 == 1) {
			res = res * x % m;
		}
		x = x * x % m;
		n /= 2;
	}
	return res;
}

int isprime(lint x) {
	//xが素数:1,xが素数でない:0
	if (x == 2) {
		return 1;
	}
	else if (x == 1 || x % 2 == 0) {
		return 0;
	}
	else {
		lint i = 3;
		while (i * i <= x) {
			if (x % i == 0) {
				return 0;
			}
			i += 2;
		}
		return 1;
	}
}

int stringTOlint(string s) {
	//string→lint
	lint Z = s.size();
	lint ret = 0;
	reverse(s);
	rep(i, Z) {
		ret += ((s[i] - '0') * pow(10, i));
	}
	return ret;
}

/*
vector<char>al(26);
al = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
vector<char>AL(26);
AL = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
vector<char>NUM(10);
	NUM = { '0','1','2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' };
*/

int main() {
	int ans = 0;
	int N;
	cin >> N;
	string S = to_string(N);
	int Z = S.size();
	vector<char>V(Z);
	rep(i, pow(3,Z)) {
		int I = i;
		rep(j, Z) {
			if (I % 3 == 0) {
				V[j] = '3';
			}
			else if (I % 3 == 1) {
				V[j] = '5';
			}
			else if (I % 3 == 2) {
				V[j] = '7';
			}
			I /= 3;
		}
		int v = 0;
		rep(j, Z) {
			v += (V[j]-'0') * pow(10, j);
		}
		int cnt3 = 0, cnt5 = 0, cnt7 = 0;
		rep(j, Z) {
			if (V[j] == '3') {
				cnt3++;
			}
			else if (V[j] == '5') {
				cnt5++;
			}
			else if (V[j] == '7') {
				cnt7++;
			}
		}
		if (v <= N && (cnt3 && cnt5 && cnt7)) {
			/*rep(j, Z) {
				cout << V[j] << ' ';
			}
			cout <<v<< endl;*/
			ans++;
		}
	}
	rep(i, Z - 1) {
		int I = i+1;
		ans += (pow(3, I) - 3 * pow(2, I) + 3);
	}
	cout << ans << endl;
}