#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int NIL = 500005;
const double EPS = 1e-9;
const int INF = 1e9;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }
long long mod = 1000000007;
long long mod2 = 1000000009;
long long power(long long base, int p) {
  if (!p) return 1;
  long long ret = power(base, p / 2);
  ret *= ret;
  ret %= mod;
  if (p & 1) ret *= base, ret %= mod;
  return ret;
}
long long modInverse(int a) { return power(a, mod - 2); }
string second;
int n;
bool can(int d) {
  int sum = 0;
  int lft = -1, walk = -1;
  for (int i = 0; i < n; i++) {
    walk--;
    if (second[i] == '*') {
      if (walk < 0) {
        if (lft == -1) lft = i;
      }
    }
    if (second[i] == 'P') {
      if (lft != -1) {
        if (i - lft > d) return 0;
        int fadl = max(d - 2 * (i - lft), (d - (i - lft)) / 2);
        if (fadl > 0)
          walk = fadl;
        else
          walk = -1;
        lft = -1;
      } else
        walk = d;
    }
  }
  return lft == -1;
}
int main() {
  cin >> n >> second;
  int low = 0, high = n * 3;
  while (low + 1 < high) {
    int mid = (low + high) / 2;
    if (can(mid))
      high = mid;
    else
      low = mid + 1;
  }
  if (can(low)) high = low;
  cout << high;
  return 0;
}
