#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void chkmax(T& x, U y) {
  if (x < y) x = y;
}
template <typename T, typename U>
inline void chkmin(T& x, U y) {
  if (y < x) x = y;
}
vector<int> a, b;
char s[110000];
bool can(int t) {
  int last = -1;
  for (int i = 0; i < a.size(); i++) {
    if (last + 1 == b.size()) return 1;
    if (b[last + 1] < a[i] - t) return 0;
    int s = 0, d = abs(a[i] - b[last + 1]);
    if (b[last + 1] < a[i])
      s = max(t - 2 * d, (t - d) / 2);
    else
      s = t;
    while (last + 1 < b.size() && b[last + 1] <= a[i] + s) last++;
  }
  return last + 1 == b.size();
}
int main() {
  int n;
  scanf("%d%s", &n, s);
  for (int i = 0; i < n; i++) {
    if (s[i] == 'P') a.push_back(i);
    if (s[i] == '*') b.push_back(i);
  }
  int lo = 0, hi = n * 2;
  while (hi - lo > 1) {
    int mid = lo + hi >> 1;
    if (can(mid))
      hi = mid;
    else
      lo = mid;
  }
  cout << hi << endl;
  return 0;
}
