import Data.Maybe
import Data.List
main=print.solve=<<readLn
solve n=length.takeWhile(n>=)$mapMaybe f[1..]
f i=if all(`elem`k)cs then Just(read.reverse$k::Int) else Nothing where
  k=unfoldr(\i->if i<=0 then Nothing else let(a,r)=divMod(i-1)3 in Just(cs!!r,a))i
cs="357"