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