module Main where import Data.List (isInfixOf) type Input = String type Output = Int parse :: String -> Input parse = head . lines solve :: Input -> Output solve input = let cond1 = isInfixOf "pain" input f ('p':'a':'i':'n' : s) = "" f (c : s) = c : f s f _ = "" g ('p':'o':'n' : s) = 1 + g s g (c : s) = g s g _ = 0 cnt = g . f $ input cond2 = cnt >= 2 in if cond1 && cond2 then cnt - 1 else -1 render :: Output -> String render = show main :: IO () main = interact ( render . solve . parse )