#include <bits/stdc++.h>
using namespace std;
int n, p[100010], q[100010], cntp, cntq;
char ch[100010];
inline bool check(int x) {
  int now = 1;
  for (int i = 1; i <= cntp; ++i) {
    if (q[now] >= p[i]) {
      if (q[now] - p[i] > x) continue;
      int pos = p[i] + x;
      while (now <= cntq && q[now] <= pos) ++now;
      if (now == cntq + 1) return 1;
    } else {
      if (p[i] - q[now] > x) continue;
      if ((p[i] - q[now]) * 3 >= x) {
        int pos = (x - (p[i] - q[now])) / 2 + p[i];
        while (now <= cntq && q[now] <= pos) ++now;
        if (now == cntq + 1) return 1;
      } else {
        int pos = x - (p[i] - q[now]) * 2 + p[i];
        while (now <= cntq && q[now] <= pos) ++now;
        if (now == cntq + 1) return 1;
      }
    }
  }
  return 0;
}
signed main() {
  cin >> n >> ch + 1;
  for (int i = 1; i <= n; ++i)
    if (ch[i] == 'P')
      p[++cntp] = i;
    else if (ch[i] == '*')
      q[++cntq] = i;
  int l = 1, r = 1e9;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l;
  return 0;
}
