#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int l = 0;
int r;
int ans = INT_MAX;
void solve(int t) {
  int last_ast = -1;
  int right_P = -1;
  for (int i = 0; i < n; i++) {
    if (s[i] == '*') {
      if (right_P >= i) {
        last_ast = -1;
      } else if (last_ast == -1) {
        last_ast = i;
      }
    } else if (s[i] == 'P') {
      if (last_ast == -1) {
        right_P = i + t;
      } else if (last_ast < i - t) {
        l = t + 1;
        return;
      } else if (last_ast + t == i) {
        right_P = i;
        last_ast = -1;
      } else {
        int sc1 = (i - last_ast - 1) * 2 + 3;
        sc1 = (t - sc1);
        int sc2 = (i - last_ast - 1) + 3;
        if (sc2 > t) {
          sc2 = -1;
        } else
          sc2 = (t - sc2) / 2;
        right_P = max(sc1, sc2) + i + 1;
        last_ast = -1;
      }
    }
  }
  if (last_ast == -1) {
    ans = min(ans, t);
    r = t - 1;
  } else {
    l = t + 1;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> s;
  r = n * 2;
  while (l <= r) {
    solve((l + r) / 2);
  }
  cout << ans << endl;
}
