#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> pp, cc;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'P') pp.push_back(i);
    if (s[i] == '*') cc.push_back(i);
  }
  int l = -1, r = 2 * n + 10;
  while (r - l > 1) {
    int md = (l + r) / 2;
    int i = 0;
    bool fl = true;
    int j = 0;
    for (auto it : pp) {
      int st = min(it, cc[j]);
      int fn = max(it, cc[j]);
      while (j < cc.size()) {
        st = min(st, cc[j]);
        fn = max(fn, cc[j]);
        int need = fn - st + min(fn - it, it - st);
        if (need > md) break;
        ++j;
      }
    }
    if (j == cc.size())
      r = md;
    else
      l = md;
  }
  cout << r << "\n";
  return 0;
}
