#include <bits/stdc++.h>
using namespace std;
vector<int> a, v;
bool check(int k) {
  int ptr = 0;
  for (int i : v) {
    for (int u = i, to = i; ptr < a.size(); ++ptr) {
      u = min(u, a[ptr]);
      to = max(to, a[ptr]);
      if (to - u + min(to - i, i - u) > k) break;
    }
  }
  return (ptr >= a.size());
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  string s;
  cin >> n >> s;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == '*')
      a.push_back(i);
    else if (s[i] == 'P')
      v.push_back(i);
  }
  int low = 0, high = 2 * n;
  while (low < high) {
    int mid = low + high >> 1;
    if (check(mid))
      high = mid;
    else
      low = mid + 1;
  }
  cout << low;
}
