#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 15;
bool can(string &s, int t) {
  int n = s.size();
  int pos = 0;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == 'P') {
      pos = min(pos, i);
      while (pos < i) {
        if (s[pos] == '*') {
          break;
        } else {
          ++pos;
        }
      }
      int l = i - pos;
      if (l > t) {
        return false;
      }
      int r = max(0, t - 2 * l);
      r = max(r, (t - l) / 2);
      pos = i + r + 1;
    }
  }
  while (pos < n) {
    if (s[pos] == '*') {
      return false;
    } else {
      ++pos;
    }
  }
  return true;
}
void solve() {
  string s;
  int n;
  cin >> n >> s;
  int l = 1, r = 3 * s.size(), mid;
  while (l < r) {
    mid = (l + r) / 2;
    if (can(s, mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  cout << l << endl;
}
int main() { solve(); }
