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