import Control.Monad.State
import Data.Map as M
import Data.Maybe (fromJust)

main = returnSequence

returnSequence :: IO ()
returnSequence = do
    getLine
    line <- getLine
    if line == "0" then return ()
                   else do
                       let (list, num) = execState transformTillFPoint (Prelude.map read.words $ line, 0)
                       print num
                       putStrLn $ unwords.Prelude.map show $ list
                       returnSequence

transformTillFPoint :: State ([Int],Int) ()
transformTillFPoint = do
    (list, num) <- get
    let newlist = replace2App list
    if list == newlist then return ()
                                else do
                                    put (newlist, num+1)
                                    transformTillFPoint

replace2App :: Ord a => [a] -> [Int]
replace2App list = Prelude.map (\x -> fromJust.M.lookup x $ ixs) list
    where ixs = countAppearance list

countAppearance :: Ord a => [a] -> Map a Int
countAppearance [] = M.empty
countAppearance (x:xs) =
    let s = countAppearance xs in
        M.insertWithKey (\k x y -> x+y) x 1 s