#include <bits/stdc++.h>
using namespace std;
int n, m;
char s[100005];
vector<int> p, star;
void input() {
  scanf("%s", s);
  p.clear(), star.clear();
  int l = strlen(s);
  for (int i = 0; i < l; i++) {
    if (s[i] == 'P') p.push_back(i);
    if (s[i] == '*') star.push_back(i);
  }
  n = p.size(), m = star.size();
}
bool can(int step) {
  int now = 0;
  for (int i = 0; i < n; i++) {
    if (now == m) return 1;
    if (p[i] - star[now] > step) return 0;
    int j = now;
    while (j < m) {
      int MIN = min(abs(p[i] - star[now]), abs(p[i] - star[j]));
      MIN += star[j] - star[now];
      if (MIN <= step)
        j++;
      else
        break;
    }
    now = j;
    if (now == m) return 1;
  }
  return 0;
}
int binary(int l, int r) {
  if (l == r) return l;
  int m = (l + r) >> 1;
  if (can(m)) return binary(l, m);
  return binary(m + 1, r);
}
int main() {
  int l;
  while (cin >> l) {
    input();
    cout << binary(0, 2 * l) << endl;
  }
  return 0;
}
