import Data.List
import Data.Maybe

main = do
  _ <- readLn :: IO Int
  es <- map (\x -> (head . head $ x, concatMap tail x)). 
        groupBy (\x y -> (head x) == (head y)).
        map (map read . words) . lines <$> getContents
  let colors = paintAll [] es 
      numColors = maximum colors
  print numColors
  mapM_ print colors

paintAll :: [(Int,Int)] -> [(Int, [Int])] -> [Int]
paintAll _ [] = []
paintAll d ((p,children):xs) = colors ++ paintAll d' xs
  where
    edges = paint d (p, children)
    colors = map snd edges
    d' = d ++ edges
   
paint :: [(Int,Int)] -> (Int, [Int]) -> [(Int, Int)]
paint dict (p, xs) = zip xs (filter (/= pColor) [1..])
  where
    pColor = fromMaybe 0 $ lookup p dict