#include using namespace std; #define rep(i,n) for(int i = 0; i < (n);i++) #define sz(x) int(x.size()) typedef long long ll; typedef long double ld; typedef pair P; int main() { int m; cin >> m; string s; s = to_string(m); int n = s.size(); int res = 0; string cur = ""; auto dfs = [&](auto& f, int len)->void{ if (len > n) return ; int t = 0; int ok = 1; for (int i = 0; i < len; i++) { int a = cur[i] - '0'; t = t * 10 + a; for (int j = i + 1; j < len; j++) { int b = cur[j] - '0'; if ((a + b) % 3 != 0) ok = 0; } } if (t <= m && len > 1) { res += ok; } if (!ok) return ; for (int d = (len ? 0 : 1); d <= 9; d++) { cur += char(d + '0'); f(f, len + 1); cur.pop_back(); } }; dfs(dfs, 0); cout << res << endl; return 0; }