#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(0); ll N; cin >> N; // 2桁 => 3の倍数 int ans = 0; for(int i = 10; i <= min(99LL, N); i++) if(i % 3 == 0) ans++; // 3桁以上 => 各桁の数字 in {0, 3, 6, 9} queue q; for(int d : {3, 6, 9}) q.push(d); while(!q.empty()) { ll x = q.front(); q.pop(); for(int d : {0, 3, 6, 9}) { x = x * 10 + d; if(x <= N) { if(100 <= x) ans++; q.push(x); } x /= 10; } } cout << ans << endl; }