#include <bits/stdc++.h>
using namespace std;
template <typename T, typename S>
inline bool REMIN(T& a, const S& b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T, typename S>
inline bool REMAX(T& a, const S& b) {
  return a < b ? a = b, 1 : 0;
}
int bit(long long x, int pos) { return (x >> pos) & 1; }
long long power(long long base, long long exp, long long c = 1e9 + 7) {
  if (!exp) return 1;
  long long r = power(base, exp >> 1, c);
  r = (r * r) % c;
  if (exp & 1) r = (r * base) % c;
  return r;
}
long long T, N, M, K;
int a, b, c;
string s1, s2;
const long double PI = 2 * acos(0);
const long long INF = 1e9 + 4;
const long long MOD = 1000000007;
const int NMAX = 3e5 + 5;
int dp[NMAX];
int last;
bool check(int t) {
  int done = -1;
  for (int(i) = int(0); (i) <= int(N - 1); ++(i)) {
    if (s1[i] == 'P') {
      int l = i;
      int til = i;
      while (til > done) {
        if (s1[til] == '*') l = til;
        til--;
      }
      til = i + 1;
      int take = i - l;
      done = i;
      if (take > t) return 0;
      int pos = 1;
      while (til < N and s1[til] != 'P') {
        if (s1[til] == '*') {
          if (min(take * 2 + til - i, take + 2 * (til - i)) <= t) {
            done = til;
          } else {
            pos = 0;
            break;
          }
        }
        til++;
      }
      if (i == last) {
        return pos;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  cin >> s1;
  int got = 0;
  for (int(i) = int(0); (i) <= int(N - 1); ++(i))
    if (s1[i] == 'P') last = i;
  int le = 0, ri = N * 3;
  while (le <= ri) {
    int m = (le + ri) / 2;
    if (check(m)) {
      a = m;
      ri = m - 1;
    } else {
      le = m + 1;
    }
  }
  cout << a;
  return 0;
}
