#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; vector ps = {0, 3, 6, 9}; long m(long x, long k) { if (x > k) { return 0; } long cnt = 1; for (long i = 0; i < 4; ++i) { cnt += m(x * 10 + ps[i], k); } return cnt; } int main() { vector xs; for (long i = 10; i < 100; ++i) { if (i % 3 == 0 and ((i / 10) + (i % 10)) % 3 == 0) { xs.push_back(i); } } long N; cin >> N; if (N < 100) { long cnt = 0; for (long i = 0; i < xs.size(); ++i) { if (xs[i] <= N) { cnt += 1; } } cout << cnt << endl; } else { long cnt = xs.size(); vector ys; for (long i = 0; i < xs.size(); ++i) { if ((xs[i] % 10) % 3 == 0 and (xs[i] / 10) % 3 == 0) { ys.push_back(xs[i]); } } cnt -= ys.size(); for (long i = 0; i < ys.size(); ++i) { cnt += m(ys[i], N); } cout << cnt << endl; } }