#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const long long inf = 1e16;
long long n, l, r, acnt = 0, pcnt = 0;
long long a[maxn], p[maxn];
bool cd = false;
inline long long read() {
  long long x = 0;
  char c = 0;
  bool flag = false;
  while (!isdigit(c)) flag |= c == '-', c = getchar();
  while (isdigit(c)) x = (x << 3ll) + (x << 1ll) + (c ^ 48), c = getchar();
  return flag ? -x : x;
}
inline long long dis(long long lef, long long rig, long long now) {
  return min(llabs(now - lef), llabs(now - rig)) + llabs(rig - lef);
}
bool check(long long x) {
  int nxt = 1, now = 1;
  for (register int i = 1; i <= pcnt; i++) {
    while (nxt <= acnt && dis(a[now], a[nxt], p[i]) <= x) nxt++;
    now = nxt;
  }
  return nxt == acnt + 1;
}
int main() {
  n = read();
  l = 0, r = n << 1ll;
  for (register int i = 1; i <= n; i++) {
    int ch = getchar();
    if (ch == '*') a[++acnt] = i;
    if (ch == 'P') p[++pcnt] = i;
  }
  while (l < r)
    check((l + r >> 1ll)) ? r = (l + r >> 1ll) : l = (l + r >> 1ll) + 1;
  printf("%I64d", l);
  return 0;
}
