import Data.Foldable (traverse_)

call n = traverse_ (\i -> putStr $ " " ++ show i) $ filter (\i -> mod3 i || inc3 i) [1..n]
  where
  mod3 i = i `mod` 3 == 0
  inc3 i = not . null $ filter (\x -> x `mod` 10 == 3) $ takeWhile (> 0) $ iterate (`div` 10) i

main = readLn >>= call >> putStrLn ""

