#include <cstdio>
#include <stack>

char S[1000001];

struct record_t {
	int top, bottom, pos;
};

int main() {
	std::stack<record_t> stack;
	scanf("%*d %s", S);
	int level = 0, pos = 1, answer = 0;
	record_t record = {0,0,0};
	stack.push(record);
	for( char *s = S; *s; s++, pos++ ) {
		if( *s == 'p' ) level++;
		else level--;

		record_t record = {level,level,pos};

		while( not stack.empty() and level >= stack.top().top ) {
			record.pos = stack.top().pos;
			record.bottom = stack.top().bottom;
			stack.pop();
		}

		while( not stack.empty() and level < stack.top().bottom ) {
			stack.pop();
		}

		if( pos-record.pos > answer )
			answer = pos-record.pos;

		stack.push(record);
	}
	printf("%d\n", answer );
}