import Data.List
main = getLine >>= print . fst . foldl' ff (0, (0, 0))

ff :: (Int, (Int, Int)) -> Char -> (Int, (Int, Int))
ff (n, (g, p)) 'g'
    | p == g = (n, (g + 1, p))
    | p < g = (n + 1, (g, p + 1))
    | otherwise = error "unreachable"
ff (n, (g, p)) _
    | p == g = (n - 1, (g + 1, p))
    | p < g = (n, (g, p + 1))
    | otherwise = error "unreachable"
