#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 4;
char s[MAXN];
int n;
vector<int> pac, str;
bool good(int t) {
  int cur = 0;
  for (int i = 0; i < (int)pac.size(); i++) {
    if (cur == (int)str.size()) return 1;
    int id = pac[i];
    int id2 = str[cur];
    int can_go;
    if (id2 < id) {
      if (id - id2 > t) return 0;
      int l = id - id2;
      if (t >= 2 * (id - id2)) {
        int can_rg = (t - l) / 2;
        can_go = min(n - 1, id + can_rg);
        can_rg = t - 2 * l;
        int can_go2 = min(n - 1, id + can_rg);
        can_go = max(can_go, can_go2);
      } else {
        int can_rg = (t - l) / 2;
        can_go = min(n - 1, id + can_rg);
      }
    } else {
      if (id2 - id > t) continue;
      can_go = min(n - 1, id + t);
    }
    while (cur < (int)str.size() && str[cur] <= can_go) {
      cur++;
    }
  }
  if (cur == (int)str.size()) return 1;
  return 0;
}
void solve() {
  cin >> n;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'P') {
      pac.push_back(i);
    }
    if (s[i] == '*') {
      str.push_back(i);
    }
  }
  int lf = 0, rg = 2 * n;
  while (lf + 1 < rg) {
    int md = (lf + rg) >> 1;
    if (good(md))
      rg = md;
    else
      lf = md;
  }
  int ans = 0;
  if (good(lf))
    ans = lf;
  else
    ans = rg;
  cout << ans;
}
int main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
