#len(s)//2-s.count("p")

iG = 0

iR = 0

for g in [int(_) for _ in list(input().rstrip().replace("g","1").replace("p","0"))]:

    if g :

        if 0 < iG:

            iR += 1

            iG -= 1

        else:

            iG += 1

    else:

        if 0 < iG:

            iG -= 1

        else:

            iR -= 1

            iG += 1

print(iR)

#len(s)//2-s.count("p")

iG = 0

iR = 0

for s in input().rstrip():

    if s == "g" :

        if 0 < iG:

            iR += 1

            iG -= 1

        else:

            iG += 1

    else:

        if 0 < iG:

            iG -= 1

        else:

            iR -= 1

            iG += 1

print(iR)

# len(s)//2-s.count("p")
iG = 0
iR = 0
for g in [int(_) for _ in list(input().rstrip().replace("g", "1").replace("p", "0"))]:
    if g:
        if 0 < iG:
            iR += 1
            iG -= 1
        else:
            iG += 1
    else:
        if 0 < iG:
            iG -= 1
        else:
            iR -= 1
            iG += 1
print(iR)

# len(s)//2-s.count("p")
iG = 0
iR = 0
for s in input().rstrip():
    if s == "g":
        if 0 < iG:
            iR += 1
            iG -= 1
        else:
            iG += 1
    else:
        if 0 < iG:
            iG -= 1
        else:
            iR -= 1
            iG += 1
print(iR)

-for g in [int(_) for _ in list(input().rstrip().replace("g", "1").replace("p", "0"))]: -    if g: +for s in input().rstrip(): +    if s == "g":