#include <bits/stdc++.h>
using namespace std;
int n;
char s[100010];
inline bool check(int mid) {
  int pos = -1, last = -1;
  for (register int i = 0; i < n; ++i) {
    if (i > last && s[i] == '*' && pos == -1) pos = i;
    if (s[i] == 'P') {
      if (pos == -1)
        last = i + mid;
      else {
        int x = i - pos;
        if (x > mid) return 0;
        if (x * 3 < mid)
          last = i + mid - (x << 1);
        else
          last = ((mid - x) >> 1) + i;
      }
      pos = -1;
    }
  }
  if (pos == -1)
    return 1;
  else
    return 0;
}
int main() {
  scanf("%d%s", &n, s);
  int l = 0, r = n << 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d", l);
  return 0;
}
