# 2. 题解

#include <bits/stdc++.h>

#define NS (1000005)
#define FIR first
#define SEC second
#define LS(a) (a << 1)
#define RS(a) (a << 1 | 1)

using namespace std;

typedef pair<int, int> PII;

int n, d[NS], lft[NS], rit[NS], ans;

char str[NS];

stack<PII> st;

struct N {int l, r, d;} e[NS << 2];

void Build(int l, int r, int a)
{
e[a].l = l, e[a].r = r;
if (l == r) {e[a].d = lft[l]; return;}
int mid = (l + r) >> 1;
Build(l, mid, LS(a)), Build(mid + 1, r, RS(a));
e[a].d = min(e[LS(a)].d, e[RS(a)].d);
}

int Query(int r, int v, int a)
{
if (e[a].l > r || e[a].d > v) return 0;
if (e[a].l == e[a].r) return e[a].l;
if (e[RS(a)].d <= v)
{
int tmp = Query(r, v, RS(a));
if (tmp) return tmp;
}
if (e[LS(a)].d <= v) return Query(r, v, LS(a));
return 0;
}

int main(int argc, char const* argv[])
{
scanf("%d%s", &n, str + 1);
for (int i = 1; i <= n; i += 1)
if (str[i] == 'p') d[i] = d[i - 1] + 1;
else d[i] = d[i - 1] - 1;
st.push(PII(INT_MAX, 0));
for (int i = 1; i <= n; i += 1)
{
while (st.top().FIR <= d[i]) st.pop();
lft[i] = st.top().SEC, st.push(PII(d[i], i));
}
while (!st.empty()) st.pop();
st.push(PII(INT_MIN, n + 1));
for (int i = n; i >= 0; i -= 1)
{
while (st.top().FIR >= d[i]) st.pop();
rit[i] = st.top().SEC, st.push(PII(d[i], i));
}
Build(1, n, 1);
for (int i = 0; i < n; i += 1)
ans = max(ans, Query(rit[i], i, 1) - i);
printf("%d\n", ans);
return 0;
}