#include <bits/stdc++.h>
using namespace std;
vector<int> stars, packmans;
bool check(int t) {
  int iStar = 0, iPackman = 0, tl, pos, posL, posR;
  for (; iPackman < (int)packmans.size() && iStar < (int)stars.size();
       iPackman++) {
    pos = packmans[iPackman];
    if (stars[iStar] < pos) {
      tl = pos - stars[iStar];
      if (t < tl) return 0;
      posL = stars[iStar];
      while (iStar < (int)stars.size() && stars[iStar] < pos) iStar++;
    } else {
      tl = 0;
      posL = pos;
    }
    if (iStar < (int)stars.size()) {
      posR = stars[iStar];
      while (
          iStar < (int)stars.size() &&
          (((t - tl) >= posR - posL) || ((t - (posR - pos)) >= posR - posL))) {
        if (++iStar < (int)stars.size()) posR = stars[iStar];
      }
    }
  }
  return iStar == (int)stars.size();
}
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  int i = 0;
  string s;
  cin >> s;
  for (char c : s) {
    if (c == '*')
      stars.push_back(i);
    else if (c == 'P')
      packmans.push_back(i);
    i++;
  }
  int l = 0, r = 3 * (n - 1) / 2 + 1;
  while (l + 1 < r) {
    int mid = (l + r) / 2;
    check(mid) ? r = mid : l = mid;
  }
  cout << (check(l) ? l : r);
  return 0;
}
