#include <bits/stdc++.h>
using namespace std;
vector<long long> heads;
vector<long long> needed;
long long n, m;
bool ok(long long time) {
  long long p = 0;
  for (long long i = 0; i < n - 1; i++) {
    if (p == m) return true;
    long long pos = heads[i];
    if (needed[p] < pos) {
      if (pos - needed[p] > time) return false;
    }
    long long lower = needed[p];
    long long higher = needed[p];
    while (p != m && abs(heads[i] - lower) + (higher - lower) <= time ||
           abs(higher - heads[i]) + (higher - lower) <= time) {
      p++;
      if (p == m) return true;
      higher = needed[p];
    }
  }
  if (p == m) return true;
  long long lower = needed[p];
  long long higher = needed[m - 1];
  if (abs(heads[n - 1] - lower) + (higher - lower) <= time ||
      abs(higher - heads[n - 1]) + (higher - lower) <= time)
    return true;
  else
    return false;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long x;
  cin >> x;
  string s;
  cin >> s;
  for (long long i = 1; i <= s.length(); i++) {
    if (s[i - 1] == '*') needed.push_back(i);
    if (s[i - 1] == 'P') heads.push_back(i);
  }
  n = heads.size();
  m = needed.size();
  long long l = 0;
  long long r = 1e18;
  while (r - l > 3) {
    long long mid = (r - l) / 2 + l;
    if (ok(mid))
      r = mid;
    else
      l = mid;
  }
  for (long long mid = l; mid <= r; mid++) {
    if (ok(mid)) {
      cout << mid << '\n';
      break;
    }
  }
}
