#include <bits/stdc++.h>
using namespace std;
int n;
char s[100005];
vector<int> pek, hrana;
bool provera(int mid) {
  int tren = 0;
  for (auto x : pek) {
    int put = mid;
    if (hrana[tren] < x) {
      if (x - hrana[tren] > mid) return false;
      put = max(mid - 2 * (x - hrana[tren]), (mid - (x - hrana[tren])) / 2);
      while (tren < hrana.size() && hrana[tren] <= x) tren++;
    }
    while (tren < hrana.size() && hrana[tren] <= x + put) tren++;
    if (tren == hrana.size()) return true;
  }
  return false;
}
int binarna(int l, int d, int sol) {
  if (l > d) return sol;
  int mid = (l + d) / 2;
  if (provera(mid)) return binarna(l, mid - 1, mid);
  return binarna(mid + 1, d, sol);
}
int main() {
  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 0; i < n; i++) {
    if (s[i] == '*')
      hrana.push_back(i + 1);
    else if (s[i] == 'P')
      pek.push_back(i + 1);
  }
  printf("%d", binarna(0, n + n + 5, -1));
  return 0;
}
