import bisect N=int(input()) d=[""] s=[] for i in range(10): d=[i+j for i in d for j in "0369"] s+=d a=set([]) for _ in s: a.add(int(_)) for i in range(12,99,3): a.add(i) b=list(a) b.sort() p=0 while N>=int(b[p]): p+=1 ans=p-4 print(ans)