#include #include using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() #define ll long long #define ld long double #define INF 1000000000000000000 typedef pair pll; typedef pair pint; ll count3(int a, bool j) { int res = 0; if (j) { for (int i = 0; i <= a; i++) { if (i % 3 == 0) res++; } } else { for (int i = 1; i <= a; i++) { if (i % 3 == 0) res++; } } return res; } ll cnt(int size) { ll res = 0; for (int i = 2; i < size - 1; i++) { res += 3 * pow(4, i); } return res; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; int ans = 0; if (N < 300) { for (int i = 10; i <= N; i++) { if (i % 3 != 0) continue; string s = to_string(i); bool judge = 1; rep(j, s.size()) { for (int k = j + 1; k < s.size(); k++) { if (((s[j] - '0') + (s[k] - '0')) % 3 != 0) { judge = 0; break; } } } if (judge) { ans++; } } } else { ans = 30; ll res = 1; string s = to_string(N); rep(i, s.size()) { if (i == 0) { res *= count3(s[i] - '0', 0); } else { res *= count3(s[i] - '0', 1); } } ans += res; ans += cnt(s.size()); } cout << ans << endl; }