from itertools import product N=int(input()) #2桁の完全な3の倍数 S=set(range(12,100,3)) #3桁以上の完全な3の倍数を追加 P=product(["3","6","9","0","",],repeat=max(3,len(str(N)))) for X in P: T="" for a in X: T+=a if len(T)>=3 and T[0]!="0": S.add(int(T)) K=0 for x in S: K+=(x<=N) print(K)