main = getContents >>= putOut.formatOut.calc.formatIn
  where putOut = putStrLn.reverse.dropWhile (\c-> c=='\n' || c==' ').reverse

formatIn :: String -> Int
formatIn = read :: String -> Int

formatOut :: [Int] -> String
formatOut = (' ':).unwords.map show

calc :: Int -> [Int]
calc n = filter f [1..n] 
  where f x = x `mod` 3==0 || elem '3' (show x)