#include #include #include #include #include #include #include #include using namespace std; const long long MOD = (long long)(1e9) + 7; map mp; long long n; int d; void dfs(string s) { if (s.size() > d) { return; } long long now = 0; for (int i = 0; i < s.size(); i++) { now *= 10LL; now += s[i] - '0'; } if (now > n) { return; } mp[s]++; dfs(s + "3"); dfs(s + "6"); dfs(s + "9"); dfs(s + "0"); return; } int main() { //cout << fixed << setprecision(15); cin >> n; d = to_string(n).size(); for (int i = 12; i <= min(99LL , n); i += 3) { mp[to_string(i)]++; } dfs("3"); dfs("6"); dfs("9"); cout << mp.size() - 3 << endl; return 0; }