unittest
{
	assert( [ "gpg" ].parse.expand.solve == 0 );
	assert( [ "ggppgggpgg" ].parse.expand.solve == 2 );
}



import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto s = input.front.to!( dchar[] );
	return tuple( s );
}

auto solve( dchar[] s )
{
	auto wc = 0L;
	auto lc = 0L;
	auto gc = 0L;
	auto pc = 0L;
	foreach( h; s )
	{
		if( pc + 1 <= gc )
		{
			// パーを出せる
			wc += ( h == 'g' );
			pc++;
		}
		else
		{
			// グーしか出せない
			lc += ( h == 'p' );
			gc++;
		}
	}
	return wc - lc;
}
