#include <bits/stdc++.h>
using namespace std;
char inp[100010];
int pac[100010], ast[100010];
int main() {
  bool possible;
  int n, moves, l, m, r, pp, pa, j, aux, ans = 3e5, jureg;
  scanf("%d", &n);
  getchar();
  scanf("%s", inp);
  pp = pa = 0;
  for (int i = 0; i < n; i++) {
    if (inp[i] == 'P')
      pac[pp++] = i;
    else if (inp[i] == '*')
      ast[pa++] = i;
  }
  l = 1, r = 3e5;
  while (l <= r) {
    m = (l + r) >> 1;
    j = 0;
    possible = true;
    for (int i = 0; i < pa; i++) {
      if (j == pp) {
        possible = false;
        break;
      }
      moves = abs(pac[j] - ast[i]);
      if (moves > m) {
        j++, i--;
        continue;
      }
      jureg = i;
      while (i + 1 < pa) {
        if (ast[i + 1] <= pac[j]) {
          i++;
          continue;
        }
        aux = abs(ast[i + 1] - pac[j]);
        if (ast[i + 1] <= pac[j] and ast[jureg] <= pac[j]) {
          if (aux < moves)
            aux = 0;
          else
            moves = 0;
        }
        if (ast[i + 1] >= pac[j] and ast[jureg] >= pac[j]) {
          if (aux < moves)
            aux = 0;
          else
            moves = 0;
        }
        if (2 * min(aux, moves) + max(moves, aux) <= m)
          i++;
        else
          break;
      }
      j++;
    }
    if (possible)
      r = m - 1, ans = min(ans, m);
    else
      l = m + 1;
  }
  l = 1, r = 3e5;
  while (l <= r) {
    m = (l + r) >> 1;
    j = pp - 1;
    possible = true;
    for (int i = pa - 1; i >= 0; i--) {
      if (j < 0) {
        possible = false;
        break;
      }
      moves = abs(pac[j] - ast[i]);
      if (moves > m) {
        j--, i++;
        continue;
      }
      jureg = i;
      while (i - 1 >= 0) {
        if (ast[i - 1] >= pac[j]) {
          i--;
          continue;
        }
        aux = abs(ast[i - 1] - pac[j]);
        if (ast[i - 1] <= pac[j] and ast[jureg] <= pac[j]) {
          if (aux < moves)
            aux = 0;
          else
            moves = 0;
        }
        if (ast[i - 1] >= pac[j] and ast[jureg] >= pac[j]) {
          if (aux < moves)
            aux = 0;
          else
            moves = 0;
        }
        if (2 * min(aux, moves) + max(moves, aux) <= m)
          i--;
        else
          break;
      }
      j--;
    }
    if (possible)
      r = m - 1, ans = min(ans, m);
    else
      l = m + 1;
  }
  printf("%d\n", ans);
  return 0;
}
