#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, nex[maxn];
char s[maxn];
bool check(int t) {
  pair<int, int> temp = make_pair(0, 0);
  for (int i = 1; i <= n; i++) {
    if (s[i] == '.') continue;
    if (s[i] == 'P')
      temp.first = i, temp.second = i;
    else {
      int d1 = temp.second - temp.first, d2 = i - temp.second;
      if (temp != make_pair(0, 0) && 2 * min(d1, d2) + max(d1, d2) <= t)
        continue;
      if (nex[i] - i > t) return false;
      temp = pair<int, int>(i, nex[i]);
      i = nex[i];
    }
  }
  return true;
}
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  int pos = 0x3f3f3f3f;
  for (int i = n; i >= 1; i--) {
    if (s[i] == '.') continue;
    if (s[i] == '*')
      nex[i] = pos;
    else
      pos = i;
  }
  int l = 0, r = 2 * n + 6, ans;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
