#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
char line[100010];
vector<int> packmens, asterisks;
bool ok(int maxTime) {
  int packman, asterisk, t, t1, t2, dist;
  for (asterisk = 0, packman = 0; packman < packmens.size(); ++packman) {
    if (packmens[packman] < asterisks[asterisk])
      t = packmens[packman] + maxTime;
    else {
      if (packmens[packman] - maxTime > asterisks[asterisk]) return false;
      dist = packmens[packman] - asterisks[asterisk];
      t1 = asterisks[asterisk] + maxTime - dist;
      t2 = packmens[packman] + (maxTime - dist) / 2;
      t = max(t1, t2);
    }
    while (asterisk < asterisks.size() && asterisks[asterisk] <= t) ++asterisk;
    if (asterisk == asterisks.size()) return true;
  }
  return false;
}
int main() {
  int i, n, l, r, mid;
  cin >> n >> line;
  for (i = 0; i < n; ++i)
    if (line[i] == 'P')
      packmens.push_back(i);
    else if (line[i] == '*')
      asterisks.push_back(i);
  for (l = 0, r = 2 * n; l < r;) {
    mid = (l + r) / 2;
    if (ok(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << '\n';
  return 0;
}
