#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> pos1;
  vector<int> pos2;
  for (int i = 0; i < n; i++) {
    if (s[i] == '*') {
      pos1.push_back(i);
    } else if (s[i] == 'P') {
      pos2.push_back(i);
    }
  }
  int l = 0, r = 1000000000;
  while (l < r) {
    int m = (l + r) >> 1;
    int p1 = 0, p2 = 0;
    int flag = 1;
    for (; p2 < pos2.size() && p1 < pos1.size(); p2++) {
      int max_right_pos = pos2[p2];
      if (pos1[p1] < pos2[p2]) {
        if (pos2[p2] - pos1[p1] > m) {
          flag = 0;
          break;
        }
        int left = m - pos2[p2] + pos1[p1];
        max_right_pos = max(max_right_pos, pos2[p2] + left / 2);
        max_right_pos =
            max(max_right_pos, pos2[p2] + left - pos2[p2] + pos1[p1]);
      } else {
        max_right_pos = pos2[p2] + m;
      }
      while (p1 < pos1.size() && pos1[p1] <= max_right_pos) p1++;
    }
    if (flag && p1 == pos1.size())
      r = m;
    else
      l = m + 1;
  }
  cout << l << endl;
}
