from itertools import product p = int(input()) n = str(10**p) keta = p+1 dp = [[[[0]*2 for i in range(3)] for j in range(2)] for k in range(keta+1)] dp[0][1][0][0] = 1 for i, j, k, l in product(range(keta), range(2), range(3), range(2)): top = int(n[i]) if j else 9 for m in range(top+1): dp[i+1][int(j and m==top)][(k+m)%3][int(l or m == 3)] += dp[i][j][k][l] ans = 0 for i in range(2): for j in range(2): ans += dp[-1][i][0][j] for j in range(3): ans += dp[-1][i][j][1] ans -= dp[-1][i][0][1] print(ans-1)