#include <bits/stdc++.h>
using namespace std;
long long int n;
string s;
bool check(long long int t) {
  long long int dest = -1, leftest = INT_MAX;
  for (long long int i = 0; i <= n - 1; i++) {
    if (s[i] == '*' && !(dest >= i)) {
      leftest = min(leftest, i);
    }
    if (s[i] == 'P') {
      if (leftest == INT_MAX)
        dest = i + t;
      else {
        if (i - leftest > t) return false;
        long long int u = i + t - 2 * (i - leftest);
        long long int v = (t + leftest + i) / 2;
        dest = max(dest, max(i, max(u, v)));
        leftest = INT_MAX;
      }
    }
  }
  if (leftest == INT_MAX) return true;
  return false;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  ;
  cin >> n;
  cin >> s;
  long long int l = 0, r = 1e7 + 10;
  while (r - l > 1) {
    long long int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  if (check(l))
    cout << l << endl;
  else
    cout << l + 1 << endl;
  return 0;
}
