#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int N = 1000000;
string str;
int main() {
  int n;
  scanf("%d", &n);
  cin >> str;
  int low = 1, mid, high = 2 * n, t, x, y, i, ans;
  while (low <= high) {
    mid = (low + high) >> 1;
    t = mid;
    i = 0, x = y = -1;
    int flagS = 0, x1 = -1, cur = 0;
    int flag = 1, posP = -1;
    for (auto a : str) {
      if (a == 'P') {
        if (flagS == 0 || x1 == -1)
          x = 0;
        else
          x = cur - x1;
        if (t < x) {
          flag = 0;
          break;
        }
        if (x == 0)
          y = t;
        else
          y = max(t - 2 * x, ((t - x) / 2));
        posP = cur;
        x1 = -1, flagS = 0;
      }
      if (a == '*') {
        if (flagS == 0) flagS = 1;
        if (flagS)
          if (cur > y + posP && x1 == -1) x1 = cur;
      }
      cur++;
    }
    if (flag)
      if (x1 != -1) flag = 0;
    if (flag) {
      ans = t;
      high = mid - 1;
    } else
      low = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
