fun readInt () = valOf (TextIO.scanStream (Int.scan StringCvt.DEC) TextIO.stdIn) fun readStr () = valOf (TextIO.inputLine TextIO.stdIn) fun readStrWithoutWS () = let val s = readStr () in String.implode (List.filter (fn c => Char.isAlphaNum c) (String.explode s)) end fun readAction () = let val numbers = List.tabulate (4, fn _ => readInt ()) val included = readStrWithoutWS () in (numbers, included) end fun member _ [] = false | member e (h::tl) = (e = h orelse member e tl) fun findAns actions = let val candidates = ref (List.tabulate (10, fn i => i)) in ( List.app (fn (numbers, included) => if included = "YES" then ( candidates := List.filter (fn n => member n numbers) (!candidates) ) else ( candidates := List.filter (fn n => (member n numbers) = false) (!candidates) ) ) actions; List.hd (!candidates) ) end val () = let val n = readInt () val actions = List.tabulate (n, fn _ => readAction ()) val ans = findAns actions in print (Int.toString ans ^ "\n") end