#include <bits/stdc++.h>
using namespace std;
char s[100010];
int len;
bool check(int mid) {
  int lastbao = -1, res = -1;
  for (int i = 0; i < len; ++i) {
    if (i > res && s[i] == '*' && lastbao == -1) lastbao = i;
    if (s[i] == 'P') {
      if (lastbao != -1) {
        int les = i - lastbao;
        if (les > mid) return 0;
        int sum = les << 1;
        if (sum < mid - les)
          res = i + mid - sum;
        else
          res = i + (mid - les) / 2;
      } else
        res = i + mid;
      lastbao = -1;
    }
  }
  return lastbao == -1;
}
int main() {
  scanf("%d\n%s", &len, s);
  int l = 0, r = 300010, ans;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid - 1;
      ans = mid;
    } else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
