#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5;
vector<int> pac, ast;
char str[MAX];
int n;
inline bool check(int limit) {
  vector<int> tmp = ast;
  reverse(tmp.begin(), tmp.end());
  for (auto &each : pac) {
    if (!tmp.empty()) {
      int to_left = each - limit;
      if (to_left > tmp.back()) {
        return false;
      }
      if (each > tmp.back()) {
        to_left = each - tmp.back();
        int total_left = 2 * to_left;
        int has = limit - to_left;
        int max_pos = 0;
        if (limit - total_left > has / 2) {
          max_pos = each + (limit - total_left);
        } else {
          max_pos = each + has / 2;
        }
        while (!tmp.empty() && tmp.back() <= max_pos) {
          tmp.pop_back();
        }
      } else {
        int to_walk = each + limit;
        while (!tmp.empty() && tmp.back() <= to_walk) {
          tmp.pop_back();
        }
      }
    }
  }
  return tmp.empty();
}
int main() {
  scanf("%d", &n);
  scanf("%s", str);
  n = strlen(str);
  for (int i = int(0); i < int(n); ++i) {
    if (str[i] == '*') {
      ast.push_back(i);
    } else if (str[i] == 'P') {
      pac.push_back(i);
    }
  }
  int left = 1, right = (int)1e6;
  int ans = -1;
  while (left <= right) {
    int mid = (left + right) / 2;
    if (check(mid)) {
      right = mid - 1;
      ans = mid;
    } else {
      left = mid + 1;
    }
  }
  printf("%d\n", ans);
  return 0;
}
