#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-10;
const long long int MOD = 1000000007ll;
const long long int mod1 = 1000000009ll;
const long long int mod2 = 1100000009ll;
int INF = (int)1e9;
long long int INFINF = (long long int)1e18;
int debug = 0;
long long int add(long long int a, long long int b, long long int m = MOD) {
  long long int x = a + b;
  while (x >= m) x -= m;
  return x;
}
long long int mul(long long int a, long long int b, long long int m = MOD) {
  long long int x = a * b;
  x %= m;
  return x;
}
const int N = (1e5) + 10;
const int LN = 18;
int n;
char a[N];
bool ok(int mid) {
  vector<int> v;
  for (int i = 0; i < n; i++) {
    if (a[i] == '*') v.push_back(i);
  }
  int ptr = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] != 'P') continue;
    int st = i;
    int ed = i;
    while (ptr < v.size()) {
      st = min(st, v[ptr]);
      ed = max(ed, v[ptr]);
      int ex = min(i - st, ed - i);
      int req = ex + ed - st;
      if (req > mid) break;
      ptr++;
    }
  }
  return ptr >= v.size();
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf(" %c ", &a[i]);
  int l = 0;
  int r = 2 * n;
  int ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (ok(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
