#include <bits/stdc++.h>
using namespace std;
char str[100010];
vector<int> f;
vector<int> p;
bool OK(int x) {
  int np = 0, nf = 0, sumf = f.size(), sump = p.size();
  while (nf < sumf && np < sump) {
    if (p[np] > f[nf]) {
      if (p[np] - f[nf] > x) return false;
      int yu1 = x - (p[np] - f[nf]);
      yu1 = yu1 / 2;
      int yu2 = x - (p[np] - f[nf]) * 2;
      int yu = max(yu1, yu2);
      while (f[nf] <= p[np] + yu) {
        nf++;
        if (nf >= sumf) break;
      }
    } else {
      while (f[nf] <= p[np] + x) {
        nf++;
        if (nf >= sumf) break;
      }
    }
    np++;
  }
  return nf >= sumf;
}
int main() {
  int n;
  scanf("%d", &n);
  getchar();
  scanf("%s", str);
  for (int i = 0; i < n; i++) {
    if (str[i] == 'P')
      p.push_back(i);
    else if (str[i] == '*')
      f.push_back(i);
  }
  int l = 0, r = 2 * n;
  while (r > l + 1) {
    int mid = (l + r) / 2;
    if (OK(mid))
      r = mid;
    else
      l = mid;
  }
  printf("%d\n", r);
  return 0;
}
