#include <bits/stdc++.h>
using namespace std;
int n, pcnt[100000 + 100];
char seq[100000 + 100];
vector<int> pac;
int md;
bool iff = false;
bool f(int in) {
  if (iff) {
    cout << "IN " << in << endl;
    getchar();
  }
  if (in >= n) return true;
  if (seq[in] == '.') return f(in + 1);
  int nxt = lower_bound(pac.begin(), pac.end(), in) - pac.begin();
  if (iff) {
    cout << "NEXT PACMAN " << nxt << ' ' << pac[nxt] << endl;
    getchar();
  }
  if (nxt == pac.size()) return false;
  int dist = pac[nxt] - in;
  if (dist > md) return false;
  int af = pac[nxt] + max(max(md - 2 * dist, 0), (md - dist) / 2) + 1;
  if (nxt == pac.size() - 1) return f(af);
  return f(min(af, pac[nxt + 1]));
}
int main() {
  scanf("%d", &n);
  scanf("%s", seq);
  for (int i = 0; i < n; i++)
    if (seq[i] == 'P') pac.push_back(i);
  int lo = 1, hi = 2 * 100000 + 1;
  while (lo < hi) {
    md = (lo + hi) / 2;
    if (f(0))
      hi = md;
    else
      lo = md + 1;
  }
  printf("%d\n", hi);
  return 0;
}
