#include <bits/stdc++.h>
const int INF = 2 * 1e9;
const long long int MOD = 1000000007;
using namespace std;
string s;
int n;
set<int> p;
set<int> f;
bool check(int c) {
  set<int> p1 = p, f1 = f;
  for (int x : p1) {
    int l = *(f1.begin());
    int d = c - abs(x - l);
    if (d < 0) continue;
    l += d;
    if (x >= l) l = x;
    vector<int> del;
    for (int y : f1) {
      if (y <= l)
        del.push_back(y);
      else
        break;
    }
    vector<int> del1;
    int d1 = d / 2 + x;
    for (int y : f1) {
      if (y <= d1)
        del1.push_back(y);
      else
        break;
    }
    if (del1 > del) {
      for (int y : del1) f1.erase(y);
    } else
      for (int y : del) f1.erase(y);
    if (f1.size() == 0) return 1;
  }
  return !(int)(f1).size();
}
void solve() {
  cin >> n;
  cin >> s;
  int l = 0, r = 1e5 * 3;
  int ans = 0;
  for (int(i) = 0; (i) < (n); (i)++) {
    if (s[i] == 'P') p.insert(i);
    if (s[i] == '*') f.insert(i);
  }
  while (r >= l) {
    int c = (r + l) / 2;
    if (check(c)) {
      ans = c;
      r = c - 1;
    } else
      l = c + 1;
  }
  cout << ans;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.fixed;
  cout.precision(12);
  solve();
  return 0;
}
