#include "bits/stdc++.h"
using namespace std;
const int MAX = 700000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

// 二項係数計算
long long COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
/*第二引数で第一引数を割ったときの切り上げの計算*/
long long int maxtime(long long int x, long long int y) {
	return(x + y - 1) / y;

}
/*最大公約数*/
long long int lcm(long long int number1, long long int number2) {
	long long int m = number1;
	long long int n = number2;

	if (number2 > number1) {
		m = number2;
		n = number1;
	}
	long long int s = -1;
	while (s != 0) {
		s = m % n;
		m = n;
		n = s;
	}
	return m;
}
/*最大公倍数*/
long long int gcd(long long int number1, long long int number2) {
	long long int m = number1;
	long long int n = number2;
	return m / lcm(m, n) * n;
}

int main() {

	long long int ans;
	cin >> ans;
	string oo = to_string(ans);
	reverse(oo.begin(), oo.end());
	long long int sum = 0;
	int a, b, c;
	a = 0;
	b = 0;
	c = 0;
	if (ans < 357) {
		cout << 0;
		return 0;
	}
	long long int keta = 0;
	long long int s = ans;
	while (s > 0) {
		s = s / 10;
		keta++;
	}
	for (int i = keta; i > 1; i--) {
		if (i == 2) {
			long long int p = ans / 100;
			for (int j = p * 100; j <= ans; j++) {
				int a1 = 0;
				int b1 = 0;
				int c1 = 0;
				int d1 = 0;
				long long int f = j;
				while (f > 0) {
					int d = f % 10;
					f = f / 10;
					if (d == 3) {
						a1 = a1 + 1;
					}
					else if (d == 5) {
						b1 = b1 + 1;
					}
					else if (d == 7) {
						c1 = c1 + 1;
					}
					else {
						d1 = d1 + 1;
					}

				}
				if (d1 > 0) {
					continue;
				}
				if ((a1 > 0) && (b1 > 0) && (c1 > 0)) {
					sum = sum + 1;
				}
			}
			break;
		}
		if ((oo[i-1]-'0') > 7) {
			int n = 0;
			if (a != 0) {
				n = n + 1;
			}
			if (b != 0) {
				n = n + 1;
			}
			if (c != 0) {
				n = n + 1;
			}
			if (n == 0) {
				sum = sum + pow(3, i) - 3 * pow(2, i) + 3;
			}
			else if (n == 1) {
				sum = sum + pow(3, i) - 2 * pow(2, i) + 1;
			}
			else if (n == 2) {
				sum = sum + pow(3, i) - pow(2, i);
			}
			else if (n == 3) {
				sum = sum + pow(3, i);
			}
			break;
		}
		if ((oo[i - 1] - '0') == 7) {
			int n = 0;
			a = a + 1;
			if (a != 0) {
				n = n + 1;
			}
			if (b != 0) {
				n = n + 1;
			}
			if (c != 0) {
				n = n + 1;
			}
			if (n == 0) {
				sum = sum + pow(3, i-1) - 3 * pow(2, i-1) + 3;
			}
			else if (n == 1) {
				sum = sum + pow(3, i-1) - 2 * pow(2, i-1) + 1;
			}
			else if (n == 2) {
				sum = sum + pow(3, i-1) - pow(2, i-1);
			}
			else if (n == 3) {
				sum = sum + pow(3, i-1);
			}
			a = a - 1;
			n = 0;
			b = b + 1;
			if (a != 0) {
				n = n + 1;
			}
			if (b != 0) {
				n = n + 1;
			}
			if (c != 0) {
				n = n + 1;
			}
			if (n == 0) {
				sum = sum + pow(3, i-1) - 3 * pow(2, i-1) + 3;
			}
			else if (n == 1) {
				sum = sum + pow(3, i-1) - 2 * pow(2, i-1) + 1;
			}
			else if (n == 2) {
				sum = sum + pow(3, i-1) - pow(2, i-1);
			}
			else if (n == 3) {
				sum = sum + pow(3, i-1);
			}
			b = b - 1;
			c = c + 1;
			continue;
		}
		if ((oo[i - 1] - '0') == 6){
			int n = 0;
			a = a + 1;
			if (a != 0) {
				n = n + 1;
			}
			if (b != 0) {
				n = n + 1;
			}
			if (c != 0) {
				n = n + 1;
			}
			if (n == 0) {
				sum = sum + pow(3, i-1) - 3 * pow(2, i-1) + 3;
			}
			else if (n == 1) {
				sum = sum + pow(3, i-1) - 2 * pow(2, i-1) + 1;
			}
			else if (n == 2) {
				sum = sum + pow(3, i-1) - pow(2, i-1);
			}
			else if (n == 3) {
				sum = sum + pow(3, i-1);
			}
			a = a - 1;
			n = 0;
			b = b + 1;
			if (a != 0) {
				n = n + 1;
			}
			if (b != 0) {
				n = n + 1;
			}
			if (c != 0) {
				n = n + 1;
			}
			if (n == 0) {
				sum = sum + pow(3, i-1) - 3 * pow(2, i-1) + 3;
			}
			else if (n == 1) {
				sum = sum + pow(3, i-1) - 2 * pow(2, i-1) + 1;
			}
			else if (n == 2) {
				sum = sum + pow(3, i-1) - pow(2, i-1);
			}
			else if (n == 3) {
				sum = sum + pow(3, i-1);
			}
			break;
		}
		if ((oo[i - 1] - '0') == 5) {
			int n = 0;
			a = a + 1;
			if (a != 0) {
				n = n + 1;
			}
			if (b != 0) {
				n = n + 1;
			}
			if (c != 0) {
				n = n + 1;
			}
			if (n == 0) {
				sum = sum + pow(3, i-1) - 3 * pow(2, i-1) + 3;
			}
			else if (n == 1) {
				sum = sum + pow(3, i-1) - 2 * pow(2, i-1) + 1;
			}
			else if (n == 2) {
				sum = sum + pow(3, i-1) - pow(2, i-1);
			}
			else if (n == 3) {
				sum = sum + pow(3, i-1);
			}
			a = a - 1;
			b = b + 1;
			continue;
		}
		if ((oo[i - 1] - '0') == 4) {
			int n = 0;
			a = a + 1;
			if (a != 0) {
				n = n + 1;
			}
			if (b != 0) {
				n = n + 1;
			}
			if (c != 0) {
				n = n + 1;
			}
			if (n == 0) {
				sum = sum + pow(3, i-1) - 3 * pow(2, i-1) + 3;
			}
			else if (n == 1) {
				sum = sum + pow(3, i-1) - 2 * pow(2, i-1) + 1;
			}
			else if (n == 2) {
				sum = sum + pow(3, i-1) - pow(2, i-1);
			}
			else if (n == 3) {
				sum = sum + pow(3, i-1);
			}
			break;
		}
		if ((oo[i - 1] - '0') == 3) {
			a = a + 1;
			continue;
		}
		if ((oo[i - 1] - '0') < 3) {
			break;
		}
	}
	for (int i = keta-1; i > 2; i--) {
		sum = sum + pow(3, i) - 3 * pow(2, i) + 3;
	}
	cout << sum;
}

Measured memory_bytes: 256