#include <bits/stdc++.h>
using namespace std;
char s[100050];
int n, a[100050], b[100050], cnt1, cnt2, cnt, ans;
bool jud(int mid) {
  int cnts = 1, cntp = 1;
  for (int i = 1; i <= n; i++) {
    if (i == b[cntp]) {
      if (b[cntp] > a[cnts]) {
        int now = b[cntp] - a[cnts];
        if (now > mid) return 0;
        int k = mid - 2 * now;
        while (a[cnts] <= max(0, k) + b[cntp]) cnts++;
        while (2 * (a[cnts] - b[cntp]) + now <= mid) cnts++;
      } else if (b[cntp] < a[cnts])
        while (a[cnts] <= mid + b[cntp]) cnts++;
      cntp++;
    }
    if (cntp == cnt2 + 1) break;
    if (cnts == cnt1 + 1) break;
  }
  return cnts == cnt1 + 1;
}
int main() {
  cin >> n;
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'P') b[++cnt2] = i;
    if (s[i] == '*') a[++cnt1] = i;
  }
  a[cnt1 + 1] = 999999999;
  int L = 0, R = 2 * n, ans;
  while (L <= R) {
    int mid = (L + R) >> 1;
    if (jud(mid))
      ans = mid, R = mid - 1;
    else
      L = mid + 1;
  }
  cout << ans;
  return 0;
}
