#include <bits/stdc++.h>
using namespace std;
vector<long long> ho, aq;
string s;
long long n;
long long cal(long long a, long long b) {
  if (a == 0 || b == 0) {
    return max(a, b);
  } else {
    return min(a * 2 + b, b * 2 + a);
  }
}
bool check(long long k) {
  long long p1 = 0, p2 = 0;
  while (p1 < ho.size()) {
    long long now = ho[p1];
    long long pe = 0, pt = 0, sum = 0;
    while (p2 < aq.size()) {
      long long p = aq[p2];
      if (p < now) {
        pe = max(pe, abs(now - p));
      } else {
        pt = max(pt, abs(now - p));
      }
      sum = cal(pe, pt);
      if (sum <= k) {
        p2++;
      } else {
        break;
      }
    }
    p1++;
  }
  return p2 == aq.size();
}
int main() {
  scanf("%d", &n);
  cin >> s;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'P') {
      ho.push_back(i);
    } else if (s[i] == '*') {
      aq.push_back(i);
    }
  }
  long long l = 1, r = 1e9, res = 1e9;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (check(mid)) {
      res = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  printf("%lld\n", res);
}
