#include #include using namespace atcoder; using namespace std; using mint = modint1000000007; using C = complex; const int mod = 998244353; const long long LINF = 1001002003004005006; const int INF = 1001001001; const double PI = acos(-1); const double EPS = 1e-10; const int di[4] = {-1,0,1,0}; const int dj[4] = {0,-1,0,1}; const int dx[8] = {1,1,1,0,0,-1,-1,-1}; const int dy[8] = {1,0,-1,1,-1,1,0,-1}; # define sz(x) (int)(x).size() # define rsz(x,n) x.resize(n) # define yosupo(x) {cout << (x) << endl; return 0;} # define ll long long # define fi first # define se second # define pb push_back # define pf push_front # define eb emplace_back # define ef emplace_front # define pob pop_back # define pof pop_front # define GET_MACRO(_1, _2, _3, NAME, ...) NAME # define _rep(i, n) _rep2(i, 0, n) # define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++) # define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__) # define srep(i, a, b) for(int i = a; i <= b; ++i) # define all(obj) (obj).begin(), (obj).end() # define rall(obj) (obj).rbegin(), (obj).rend() inline void YesNo(bool f) { std::cout << (f? "Yes": "No") << std::endl; } void read() {} template void read(T &t, U &...u) { cin >> t; read(u...); } void writeln() { cout << endl; } template void writeln(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; writeln(u...); } # define Pll pair # define P pair # define bit(x,i) (((x) >> (i)) & 1) # define equals(a, b) (fabs((a) - (b)) < EPS) // 誤差を考慮した同値判定 templatebool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } templateistream& operator>>(istream&i,vector&v){rep(j,v.size())i>>v[j];return i;} templatestring join(vector&v){stringstream s;rep(i,v.size())s<<' '<ostream& operator<<(ostream&o,vector&v){if(v.size())o<ostream& operator<<(ostream&o,vector>&vv){if(vv.size())o< ostream& operator<<(ostream& os, const pair& p) { return os << "P(" << p.first << " " << p.second << ")";} template using vc = vector; template using vv = vc>; using vi = vc; using vvi = vv; using vl = vc; using vvl = vv; using vm = vc; using vvm = vv; //桁数, 桁和のmod3, 3があるか, 以下フラグ ll dp[20][3][2][2]; int main() { int p; read(p); string s = "1"; rep(i,p) s += "0"; int n = sz(s); dp[0][0][0][0] = 1; rep(i,n) { int nd = s[i]-'0'; rep(j,3) { rep(t,2) { if(t == 0) { int nj = (j+nd)%3; if(nd == 3) { dp[i+1][nj][1][0] += dp[i][j][0][0]; dp[i+1][nj][1][0] += dp[i][j][1][0]; } else { dp[i+1][nj][0][0] += dp[i][j][0][0]; dp[i+1][nj][1][0] += dp[i][j][1][0]; } } else { rep(k,nd) { if(k == 3) { int nj = (j+k)%3; dp[i+1][nj][1][1] += dp[i][j][0][0]; dp[i+1][nj][1][1] += dp[i][j][1][0]; } else { int nj = (j+k)%3; dp[i+1][nj][0][1] += dp[i][j][0][0]; dp[i+1][nj][1][1] += dp[i][j][1][0]; } } rep(k,10) { if(k == 3) { int nj = (j+k)%3; dp[i+1][nj][1][1] += dp[i][j][0][1]; dp[i+1][nj][1][1] += dp[i][j][1][1]; } else { int nj = (j+k)%3; dp[i+1][nj][0][1] += dp[i][j][0][1]; dp[i+1][nj][1][1] += dp[i][j][1][1]; } } } } } } ll ans = 0; rep(i,3) rep(j,2) rep(k,2) { if(i == 0 || j == 1) ans += dp[n][i][j][k]; } writeln(ans-1); }