fun readStr () = let fun scan reader stream = SOME (StringCvt.splitl (not o Char.isSpace) reader (StringCvt.skipWS reader stream)) in valOf (TextIO.scanStream scan TextIO.stdIn) end fun intString n = if 0 <= n then Int.toString n else "-" ^ Int.toString (abs n) fun findAns n = let val exploded = String.explode n val first = List.hd exploded val threes = List.tl exploded exception NotThree fun findAnsAux nil = 0 | findAnsAux (#"3" :: tl) = 1 + findAnsAux tl | findAnsAux (_ :: _) = raise NotThree in if first = #"1" then ( (findAnsAux threes) handle NotThree => ~1 ) else ( ~1 ) end val () = let val n = readStr () val ans = findAns n in print (intString ans ^ "\n") end