#include <bits/stdc++.h>
const int N = rand() % 1141;
using namespace std;
int n;
string s;
bool can(int time) {
  vector<int> current, fix, dist_last;
  dist_last.resize(n, 0);
  for (int i = 0; i < n; i++) {
    if (s[i] == 'P') {
      int last = 0;
      while ((int)(fix).size()) {
        int distance = i - fix.back();
        if (distance > time) break;
        last = distance;
        fix.pop_back();
      }
      current.push_back(i);
      dist_last[i] = last;
    }
    if (s[i] == '*') {
      fix.push_back(i);
      if (!current.empty()) {
        int distance = i - current.back();
        if (max(dist_last[current.back()], distance) +
                2 * min(dist_last[current.back()], distance) <=
            time) {
          fix.pop_back();
        }
      }
    }
  }
  return (int)(fix).size() == 0;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  ;
  cin >> n;
  cin >> s;
  int l = 0, r = 10000000, c = r;
  while (l <= r) {
    int m = (l + r) / 2;
    if (can(m)) {
      c = m;
      r = m - 1;
    } else {
      l = m + 1;
    }
  }
  cout << c << "\n";
  return 0;
}
