#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N],b[N],n;
stack<int>q;
char c[N];
int main(){
cin>>n;
int sum=0;
a[0]=0;
for(int i = 1;i <= n;i++){
cin>>c[i];
if(c[i]=='p') a[i]=a[i-1]+1;
else a[i]=a[i-1]-1;
}
q.push(0);
for(int i = n;i >= 0;i--){
while(!q.empty() && a[q.top()]>a[i]) q.pop();
if(!q.empty()) b[i]=q.top();
if(q.empty() || q.top()==0) b[i]=n+1;
q.push(i);
}
for(int i = 1;i <= n;i++){
if(c[i]=='j') continue;
int ans=b[i-1]-i+1;
while(c[b[i]]=='j' && b[i]>=i) ans--,b[i]--;
sum=max(sum,ans);
}
cout<<sum;
}