#include <bits/stdc++.h>
using namespace std;
const int N = 500011;
int n, cnt = 0;
string k;
vector<int> art, pc;
int lastOne = 0;
bool can(int curPos, int dist, int idx) {
  int l = art[lastOne + 1];
  int r = art[idx];
  if (r <= curPos) return curPos - l <= dist;
  if (l == r) return abs(l - curPos) <= dist;
  if (l >= curPos) return r - curPos <= dist;
  int mn = min(curPos - l, r - curPos);
  int mx = max(curPos - l, r - curPos);
  return 2 * mn + mx <= dist;
}
bool check(int mid) {
  lastOne = -1;
  for (int i = 0; i < pc.size(); ++i) {
    int lo = lastOne + 1, hi = ((int)art.size()) - 1, best = -1;
    while (lo <= hi) {
      int m2 = (lo + hi) / 2;
      if (can(pc[i], mid, m2)) {
        best = m2;
        lo = m2 + 1;
      } else {
        hi = m2 - 1;
      }
    }
    lastOne = max(lastOne, best);
  }
  int need = (int)art.size();
  return lastOne >= need - 1;
}
int main() {
  cin >> n;
  cin >> k;
  for (int i = 0; i < k.size(); ++i) {
    if (k[i] == '*') art.push_back(i);
    if (k[i] == 'P') pc.push_back(i);
  }
  int lo = 0, hi = 2 * n, best = -1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (check(mid)) {
      best = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  printf("%d\n", best);
  return 0;
}
