#include using namespace std; int main() { int N; cin >> N; int ans = 0; function dfs = [&](long n) -> void { if (n > N) return; if (n > 9) ans++; string s = to_string(n); for (int next = 0; next <= 9; next++) { for (int i = 0; i < s.size(); i++) { int tmp = s.at(i) - '0'; if ((tmp + next) % 3) goto NG; } dfs(n * 10 + next); NG:; } }; for (int i = 1; i <= 9; i++) dfs(i); cout << ans << "\n"; }