#include <bits/stdc++.h>
int N;
char map[100001];
int psz, ssz;
std::vector<int> pack, star;
int abs(int a) { return a < 0 ? -a : a; }
int max(int a, int b) { return a < b ? b : a; }
bool isOk(int mv) {
  int cnt = 0, si = 0, pi = 0;
  for (int pi = 0; pi < psz && si < ssz; pi++) {
    int spos = star[si];
    int ppos = pack[pi];
    int gap = abs(ppos - spos);
    int maxpos = 0;
    if (gap <= mv) {
      if (spos < ppos)
        maxpos = ppos + max(mv - gap * 2, (mv - gap) / 2);
      else
        maxpos = ppos + mv;
      while (si < ssz && star[si] <= maxpos) {
        cnt++;
        si++;
      }
    }
  }
  return cnt == ssz;
}
int main() {
  scanf("%d%s", &N, map);
  pack.clear();
  star.clear();
  for (int i = 0; i < N; i++)
    if (map[i] == 'P')
      pack.push_back(i);
    else if (map[i] == '*')
      star.push_back(i);
  psz = pack.size();
  ssz = star.size();
  int s = 0, e = N + N, m;
  int ans = e;
  while (s <= e) {
    m = (s + e) / 2;
    if (isOk(m)) {
      ans = m;
      e = m - 1;
    } else
      s = m + 1;
  }
  printf("%d", ans);
}
