#include <bits/stdc++.h>
using namespace std;
char s[100005];
int le[100005], ri[100005];
int cl[100005], cr[100005];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  int lo = 1, hi = 2 * n, ans = 0;
  int cur = -1;
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'P') cur = i;
    if (s[i] == '*') le[i] = cur;
  }
  cur = -1;
  for (int i = n; i >= 1; i--) {
    if (s[i] == 'P') cur = i;
    if (s[i] == '*') ri[i] = cur;
  }
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    for (int i = 1; i <= n; i++)
      if (s[i] == 'P') cl[i] = 0, cr[i] = 0;
    bool ok = 1;
    for (int i = 1; i <= n; i++) {
      if (s[i] != '*') continue;
      bool p = 0;
      if (le[i] != -1) {
        int tmp = cr[le[i]];
        cr[le[i]] = max(cr[le[i]], abs(i - le[i]));
        int mn = min(cl[le[i]], cr[le[i]]);
        int mx = max(cl[le[i]], cr[le[i]]);
        if (2 * mn + mx <= mid)
          p = 1;
        else
          cr[le[i]] = tmp;
      }
      if (!p && ri[i] != -1) {
        int tmp = cl[ri[i]];
        cl[ri[i]] = max(cl[ri[i]], abs(i - ri[i]));
        int mn = min(cl[ri[i]], cr[ri[i]]);
        int mx = max(cl[ri[i]], cr[ri[i]]);
        if (2 * mn + mx <= mid)
          p = 1;
        else
          cl[ri[i]] = tmp;
      }
      ok &= p;
      if (!ok) break;
    }
    if (ok)
      ans = mid, hi = mid - 1;
    else
      lo = mid + 1;
  }
  printf("%d\n", ans);
}
