#include using namespace std; #define int long long typedef pair P; int INF = 1e16+7; int mod = 998244353; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; signed main() { int N; cin >> N; int cnt = 0; for(int i = 10; i <= min(99LL,N); i++) { if((i/10+i%10)%3 == 0 && i%3 == 0) { cnt++; } } string S = to_string(N); for(int i = 2; i < S.size()-1; i++) { cnt += 3*pow(4,i); } if(S.size() >= 3) { cnt += ((S[0]-'0')-1)/3*pow(4,S.size()-1); } if((S[0]-'0')%3 == 0 && S.size() >= 3) { for(int i = 1; i < S.size(); i++) { if(S[i]-'0' >= 1 && i+1 != S.size()) { cnt += (1+(S[i]-'0'-1)/3)*pow(4,S.size()-i-1); } if(i+1 == S.size()) { cnt += 1+(S[i]-'0')/3; } if((S[i]-'0')%3) { break; } } } cout << cnt << endl; }