import Control.Monad
import Data.IORef
main = do
    n <- readLn :: IO Int
    --
    x <- newIORef 0;
    forM_ [1..n] $ \i -> do
        writeIORef x i
        if (i `mod` 3 == 0) then
            putStr $ " " ++ show i
        else
            include3 i x
    putStrLn ""
    where
    include3 :: Int -> IORef Int -> IO ()
    include3 i x = do
        x' <- readIORef x
        if (x' `mod` 10 == 3) then
            putStr $ " " ++ show i
        else do
            x' <- readIORef x
            writeIORef x (x' `div` 10)
            readIORef x
            >>= \a -> when (a /= 0) $
                include3 i x