#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> stars, pacmans;
  for (int i = 0; i < n; i++) {
    if (s[i] == '*')
      stars.push_back(i);
    else if (s[i] == 'P')
      pacmans.push_back(i);
  }
  stars.push_back(1e9);
  int l = 1, r = 2 * n, ok = 1;
  while (l <= r) {
    int mid = (l + r) / 2;
    int start = stars[0];
    int curr = 0;
    bool f = true;
    for (int x : pacmans) {
      if (start < x - mid)
        f = false;
      else if (start < x) {
        curr = max(x + mid - (x - start) * 2, x + (mid - x + start) / 2);
      } else {
        curr = x + mid;
      }
      start = *upper_bound(stars.begin(), stars.end(), curr);
    }
    if (!f or start != 1e9) {
      l = mid + 1;
    } else {
      ok = mid;
      r = mid - 1;
    }
  }
  cout << ok << endl;
  return 0;
}
