#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>

#define rep(x,to) for(int (x)=0;(x)<(to);(x)++)

using namespace std;
typedef long long ll;


ll cnt(int n){
	string s = "1" + string(n, '0'); n++;
	ll dp[22][2][3][2]={};
	dp[0][0][0][0] = 1; //dp[桁数][3有り][%3][未/確] = 数
	rep(i,n)  rep(j, 2) rep(k, 3) rep(d, 10){
		int mj = j | (d==3);
		int mk = (k + d)%3;
		if(d< s[i]-'0')  dp[i+1][mj][mk][1] += dp[i][j][k][0];
		if(d==s[i]-'0')  dp[i+1][mj][mk][0] += dp[i][j][k][0];
		dp[i+1][mj][mk][1] += dp[i][j][k][1];
	}
	
	ll ans = 0;
	rep(i,2) rep(j,3) if(i==1 || j==0) ans += dp[n][i][j][0] + dp[n][i][j][1];
	return ans - 1; // -1 zero?
}

int main()
{
	int p; cin >>p;
	cout << cnt(p) << endl;
	return 0;
}