#include using namespace std; #define rep(i,a,n) for(int i=a; i=n; i--) typedef long long int ll; typedef pair pii; template void chmax(T &a, T b) {a = max(a, b);} template void chmin(T &a, T b) {a = min(a, b);} // digit, mod, has3, flag ll dp[25][3][2][2]; int main() { int n; cin >> n; string s(n, '0'); s = '1' + s; int m = s.length(); dp[0][0][0][0] = 1; rep(i,0,m) rep(j,0,3) rep(k,0,2) rep(l,0,2) { int lim = (l == 1 ? 9 : s[i] - '0'); rep(x,0,lim + 1) { int mo = (j + x) % 3; dp[i+1][mo][k || (x == 3)][l || x < lim] += dp[i][j][k][l]; } } ll ans = 0; rep(j,0,3) rep(k,0,2) rep(l,0,2) { if(j == 0 || k == 1) ans += dp[m][j][k][l]; } cout << ans - 1 << endl; return 0; }