#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; } if ((long long)stoi(s) > 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; }