import Data.List

main = interact $ unlines . map (show' . puzzle . map (read . return)) . lines
 where show' xs = if null xs then "0" else unwords $ map show xs

puzzle tehai = filter is_machi [1..9]
 where
  is_machi n
   | (length $ filter (== n) tehai) >= 4 = False
   | otherwise = is_agari (sort (n:tehai))
  is_agari ns = any of_atama cand_atama
   where
    of_atama n = is_4mentsu (Just (ns \\ [n,n]))
    cand_atama = map head $ filter ((>=2) . length) $ group $ ns
  
  is_4mentsu Nothing = False
  is_4mentsu (Just []) = True
  is_4mentsu (Just ns'@(n1:n2:n3:ns))
   | (n1==n2) && (n2==n3) = is_4mentsu (Just ns) || is_4mentsu (pick_shuntsu ns')
   | otherwise =  is_4mentsu (pick_shuntsu ns')
  
  pick_shuntsu (n:ns)
   | elem (n+1) ns && elem (n+2) ns = Just (ns \\ [n+1,n+2])
   | otherwise = Nothing