#include <bits/stdc++.h>
using namespace std;
bool qry(vector<int>& a, vector<int>& b, int k) {
  int t = 0;
  for (int i = 0; i < a.size(); i++) {
    if (t == b.size() || b[t] - a[i] > k) continue;
    if (a[i] - b[t] > k) return 0;
    int j =
        (a[i] > b[t] ? max((k - a[i] + b[t]) / 2 + a[i], k - a[i] + 2 * b[t])
                     : k + a[i]);
    t = upper_bound(b.begin(), b.end(), j) - b.begin();
  }
  return (t == b.size());
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> a, b;
  for (int i = 0; i < n; i++) {
    if (s[i] == '*')
      b.push_back(i);
    else if (s[i] == 'P')
      a.push_back(i);
  }
  int l = 1, r = n + n / 2 + 1;
  while (l <= r) {
    int mid = l + (r - l) / 2;
    if (qry(a, b, mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  cout << l << endl;
}
