#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9, inf = 0x3f3f3f3f;
int n, m, lst;
char s[N];
struct node {
  int lf, pl, lr;
} a[N], f[N];
bool check(int x) {
  for (int i = (1); i <= (m); i++) a[i] = f[i];
  a[m + 1].lf = a[m + 1].lr = -1;
  for (int i = (1); i <= (m); i++) {
    if (a[i].lr) {
      for (int j = (a[i].lr); j <= (a[i].pl); j++) {
        if (s[j - 1] != '*')
          a[i].lr++;
        else
          break;
      }
      if (a[i].lr == a[i].pl) {
        a[i].lr = 0, a[i + 1].lr = a[i + 1].lf <= a[i].pl + x
                                       ? 0
                                       : max(a[i + 1].lr, a[i].pl + x + 1);
        continue;
      }
      if (x < a[i].pl - a[i].lr) return false;
      int num = max(x - 2 * (a[i].pl - a[i].lr), (x - a[i].pl + a[i].lr) / 2);
      if (num > 0) {
        if (i == m)
          a[i + 1].lr = a[i].pl + num;
        else
          a[i + 1].lr = a[i + 1].lf <= a[i].pl + num
                            ? 0
                            : max(a[i + 1].lr, a[i].pl + num + 1);
      }
    } else {
      if (i == m)
        a[i + 1].lr = a[i].pl + x;
      else
        a[i + 1].lr =
            a[i + 1].lf <= a[i].pl + x ? 0 : max(a[i + 1].lr, a[i].pl + x + 1);
    }
  }
  if (max(a[m + 1].lr, a[m].pl) >= lst + 1) return true;
  return false;
}
int erfen(int l, int r) {
  int res = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid))
      res = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  return res;
}
int main() {
  scanf("%d%s", &n, s);
  int now1 = 0, now2 = 0;
  for (int i = (0); i <= (n - 1); i++) {
    if (s[i] == 'P')
      a[++m].lf = now2 ? now1 : 0, a[m].pl = i + 1, a[m].lr = now2, now2 = 0;
    else if (s[i] == '*')
      now1 = i + 1, now2 = now2 ? now2 : i + 1, lst = i;
  }
  for (int i = (1); i <= (m); i++) f[i] = a[i];
  printf("%d", erfen(0, 2 * n));
  return 0;
}
