#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e15;
const long double eps = 1e-9;
const long double pi = 2 * acos(0.0);
void body();
int main() { body(); }
const int N = 300000;
int n;
string s;
vector<int> p;
int cost(int l, int r, int p) {
  if (p < l || p > r) {
    return max(abs(p - l), abs(p - r));
  }
  return r - l + min(abs(p - l), abs(p - r));
}
int bin(int maks) {
  int L = 0, R = 0, k = 0;
  while (k < p.size()) {
    while (L < n && s[L] != '*') {
      L += 1;
    }
    if (L >= n) {
      break;
    }
    R = L - 1;
    if (k + 1 < p.size() && L >= p[k + 1]) {
      k += 1;
    }
    int pac = p[k];
    while (R + 1 < n && cost(L, R + 1, pac) <= maks) {
      R += 1;
    }
    k += 1;
    L = R + 1;
  }
  while (L < n && s[L] != '*') {
    L += 1;
  }
  return (L >= n);
}
void body() {
  cin >> n >> s;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'P') {
      p.emplace_back(i);
    }
  }
  int l = 0, r = n + n, res = -1;
  while (l <= r) {
    int m = (l + r) >> 1;
    if (bin(m)) {
      res = m;
      r = m - 1;
    } else {
      l = m + 1;
    }
  }
  cout << res << "\n";
}
