#include <bits/stdc++.h>
using namespace std;
const int INF = numeric_limits<int>::max();
const long long LLINF = numeric_limits<long long>::max();
const unsigned long long ULLINF = numeric_limits<unsigned long long>::max();
const double PI = acos(-1.0);
vector<int> p;
vector<int> stars;
int dots[100010];
int n;
inline int sum(int l, int r) {
  if (l > r) return 0;
  return r - l + 1;
}
int go(int l, int r, int val) {
  if (l >= r) return l;
  int L = l + 1;
  int res = l;
  while (l <= r) {
    int m = (l + r) >> 1;
    if (sum(L, m) <= val)
      res = m, l = m + 1;
    else
      r = m - 1;
  }
  return res;
}
bool pos(int val) {
  int ptr = 0;
  int L = stars[0];
  for (int i = 0; i < p.size(); i++) {
    if (sum(L, p[i] - 1) > val) return false;
    int v = val - sum(L, p[i] - 1) - max(0, p[i] - L);
    int R = p[i];
    if (v >= 0) {
      R = go(p[i], (i + 1 < p.size() ? p[i + 1] : stars.back()), v);
    }
    int l = 0, r = (i + 1 < p.size() ? p[i + 1] : stars.back()) - p[i], res = 0;
    while (l <= r) {
      int m = (l + r) >> 1;
      if (sum(L, p[i] + m - 1) + sum(p[i] + 1, p[i] + m) <= val) {
        res = p[i] + m, l = m + 1;
      } else
        r = m - 1;
    }
    R = max(R, res);
    while (ptr < stars.size() && stars[ptr] <= R) ptr++;
    if (ptr == stars.size()) return true;
    L = stars[ptr];
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == '.')
      dots[i] = 1;
    else if (s[i] == 'P')
      p.push_back(i);
    else
      stars.push_back(i), dots[i] = 1;
  }
  for (int i = 1; i < n; i++) dots[i] += dots[i - 1];
  int l = 1, r = 4 * n, res = -1;
  while (l <= r) {
    int m = (l + r) >> 1;
    if (pos(m)) {
      res = m;
      r = m - 1;
    } else
      l = m + 1;
  }
  cout << res;
  return 0;
}
