#include <bits/stdc++.h>
using namespace std;
int n, nt[200005], l[200005], r[200005], lt[200005], Now;
char s[200005];
inline int read() {
  int ret = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch <= '9' && ch >= '0')
    ret = (ret << 1) + (ret << 3) + (ch ^ 48), ch = getchar();
  return ret;
}
inline bool check(int mid) {
  for (register int i = 1; i <= n; ++i)
    if (s[i] == 'P') l[i] = r[i] = i;
  for (register int i = 1; i <= n; ++i)
    if (s[i] == '*') {
      if (lt[i] && min(2 * (lt[i] - l[lt[i]]) + i - lt[i],
                       2 * (i - lt[i]) + lt[i] - l[lt[i]]) <= mid) {
        r[lt[i]] = i;
        continue;
      }
      if (nt[i] && l[nt[i]] < i) continue;
      if (!nt[i] || nt[i] - i > mid) return 0;
      l[nt[i]] = i;
    }
  return 1;
}
int main() {
  n = read();
  scanf("%s", s + 1);
  int l = 0, r = 1e7;
  for (register int i = 1, now = 0; i <= n; ++i)
    if (s[i] == 'P')
      now = i;
    else if (s[i] == '*')
      lt[i] = now;
  for (register int i = n, now = 0; i; --i)
    if (s[i] == 'P')
      now = i;
    else if (s[i] == '*')
      nt[i] = now;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d", l);
  return 0;
}
