#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
char s[maxn], ss[maxn];
bool judge(int x, int n) {
  queue<int> q;
  for (int i = 1; i <= n; i++)
    if (s[i] == 'P') q.push(i);
  int start = 0, end = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '.') continue;
    if (s[i] == '*') {
      if (start <= end)
        start = i;
      else
        continue;
    }
    if (s[i] == 'P') {
      q.pop();
      if (start <= end) {
        end = i + x;
        if (!q.empty()) end = min(end, q.front() - 1);
        i = end;
      } else {
        if (i - start > x) return false;
        end = max(i + (x - i + start) / 2, i + x - 2 * (i - start));
        if (!q.empty()) end = min(end, q.front() - 1);
        i = end;
      }
    }
  }
  return end >= start;
}
int main() {
  int n;
  while (scanf("%d", &n) == 1) {
    scanf(" %s", s + 1);
    for (int i = 1; i <= n; i++) ss[i] = s[n - i + 1];
    int l = 0, r = 2 * n, mid;
    while (l < r) {
      mid = (l + r) / 2;
      if (judge(mid, n))
        r = mid;
      else
        l = mid + 1;
    }
    printf("%d\n", l);
  }
  return 0;
}
