#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long modexp(long long x, long long n) {
  if (n == 0) return 1;
  if (n % 2 == 0) {
    long long y = modexp(x, n / 2) % mod;
    return (y * y) % mod;
  }
  return (x * modexp(x, n - 1) % mod) % mod;
}
char s[100005];
vector<int> pman, food;
bool check(int t) {
  int temp = t;
  int j = 0;
  int cnt = 0;
  int sz = food.size();
  if (sz == 0) return true;
  for (int i = 0; i < pman.size(); i++) {
    t = temp;
    if (food[j] < pman[i]) {
      if (t < (pman[i] - food[j])) {
        return false;
      }
      int t1 = (pman[i] - food[j]);
      int c1 = 0, c2 = 0, c3 = 0;
      while (j < sz && food[j] < pman[i]) {
        c1++;
        j++;
        cnt++;
      }
      int k = j;
      while (k < sz && (2 * (food[k] - pman[i]) + t1) <= t) {
        c2++;
        k++;
      }
      int k1 = j;
      while (k1 < sz && ((food[k1] - pman[i]) + 2 * t1) <= t) {
        c3++;
        k1++;
      }
      if (c2 > c3) {
        t -= (t1 + 2 * (food[k - 1] - pman[i]));
        j = k;
        cnt += c2;
      } else {
        t -= (2 * t1 + (food[k - 1] - pman[i]));
        j = k1;
        cnt += c3;
      }
      if (j == sz) return true;
      continue;
    }
    if (j == sz) return true;
    if (t > 0) {
      int curr = pman[i];
      while (1) {
        if (t < (food[j] - curr)) {
          break;
        }
        t -= (food[j] - curr);
        curr = food[j];
        j++;
        cnt++;
        if (j == sz) return true;
      }
    }
  }
  if (cnt == sz) return true;
  return false;
}
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s);
  for (int i = 0; i < n; i++) {
    if (s[i] == 'P') {
      pman.push_back(i);
    } else if (s[i] == '*') {
      food.push_back(i);
    }
  }
  int low = 0;
  int high = 2 * n;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (check(mid) == false) {
      low = mid + 1;
    } else {
      if (mid == 0) {
        printf("0\n");
        return 0;
      }
      if (check(mid - 1) == false) {
        printf("%d\n", mid);
        return 0;
      } else
        high = mid - 1;
    }
  }
  return 0;
}
