#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n;
char s[100005];
int check(int t) {
  queue<int> x, p;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '*')
      x.push(i);
    else if (s[i] == 'P')
      p.push(i);
  }
  while (p.size()) {
    int now = p.front();
    p.pop();
    if (x.size() == 0) return 1;
    if (x.front() < now) {
      int first = x.front();
      while (x.size() && x.front() <= now) {
        x.pop();
      }
      int need = now - first;
      if (need > t) return 0;
      int last = t - need;
      if (last) {
        int next = now + last / 2;
        int tnext = now + (t - need * 2);
        next = max(next, tnext);
        while (x.size() && x.front() <= next) {
          x.pop();
        }
      }
    } else {
      int last = min(n, now + t);
      while (x.size() && x.front() >= now && x.front() <= last) {
        x.pop();
      }
    }
  }
  if (x.size() == 0) return 1;
  return 0;
}
int solve() {
  int l = 0, r = 500005;
  while (l < r) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  return l;
}
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  printf("%d\n", solve());
  return 0;
}
