#include <bits/stdc++.h>
using namespace std;
long long n, i, comp, ans, lo, hi, l, r, s;
char c[100010];
vector<long long> pack, star;
bool ok(long long mx) {
  comp = 0;
  for (long long p : pack) {
    l = r = p;
    for (i = comp; i < star.size();) {
      s = star[i];
      r = max(s, r);
      l = min(l, s);
      if (r - l + min(r - p, p - l) > mx) break;
      comp++;
      i++;
    }
  }
  return comp == (long long)star.size();
}
int32_t main() {
  cin >> n >> c;
  for (i = 0; i < n; i++) {
    if (c[i] == 'P')
      pack.push_back(i);
    else if (c[i] == '*')
      star.push_back(i);
  }
  lo = 1, hi = 2 * n + 1, ans = -1;
  while (lo <= hi) {
    long long mid = (lo + hi) / 2;
    if (ok(mid)) {
      ans = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << ans << "\n";
}
