#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6;
int n;
string s;
vector<int> ss, ps;
bool check(int x) {
  int j = 0;
  for (int i = 0; i < ps.size(); i++) {
    if (j >= ss.size()) break;
    int l = max(0, ps[i] - ss[j]), r = 0;
    if (l > x) return false;
    while (j < ss.size() && ss[j] < ps[i]) {
      j++;
    }
    int time = l;
    while (j < ss.size()) {
      r = ss[j] - ps[i];
      if (l < r) {
        time = (2 * l) + r;
      } else {
        time = (2 * r) + l;
      }
      if (time > x) break;
      j++;
    }
  }
  if (j != ss.size()) return false;
  return true;
}
int main() {
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'P') {
      ps.push_back(i);
    } else if (s[i] == '*') {
      ss.push_back(i);
    }
  }
  int left = 0, right = MAXN;
  while (left < right) {
    int mid = (left + right) / 2;
    if (!check(mid)) {
      left = mid + 1;
    } else {
      right = mid;
    }
  }
  cout << right << endl;
}
