import Control.Monad
import qualified Data.ByteString.Char8 as C

main = sol . C.filter (liftM2 (||) ('p'==) ('g'==)) <$> C.getContents >>= print

sol = fst . C.foldl' f (0,0)

f (s,0) 'p' = (s-1,1) 
f (s,h) 'p' = (s,h-1)
f (s,0) 'g' = (s,1) 
f (s,h) 'g' = (s+1,h-1)
