import Data.Functor ((<$>))

digits :: Int -> [Int]
digits n = go (abs n) []
  where go n' ds
          | n' < 10   = n' : ds
          | otherwise = go (n' `div` 10) ((abs n' `mod` 10) : ds)

main :: IO ()
main = do
  n <- (read :: String -> Int) <$> getLine
  putStrLn . (" " ++) . unwords . map show $ filter p [1..n]
    where p x = x `mod` 3 == 0 || 3 `elem` digits x

