#include <bits/stdc++.h>
using namespace std;
using pi = pair<int, int>;
using pd = pair<double, double>;
bool works(int time, vector<int> eaters, deque<int> food) {
  for (int i : eaters) {
    vector<int> have_to_eat;
    while (!food.empty() && food[0] <= i) {
      have_to_eat.push_back(food[0]);
      food.pop_front();
    }
    if (have_to_eat.empty()) {
      while (!food.empty() && food[0] - i <= time) {
        food.pop_front();
      }
    } else {
      if (i - have_to_eat[0] > time) {
        return false;
      }
      int left_time = i - have_to_eat[0];
      int right_first_time = time - left_time;
      int right_second_time = time - 2 * left_time;
      int right_first_count = 0, left_first_count = 0;
      while (right_first_count < food.size() &&
             2 * (food[right_first_count] - i) <= right_first_time) {
        right_first_count++;
      }
      while (left_first_count < food.size() &&
             food[left_first_count] - i <= right_second_time) {
        left_first_count++;
      }
      int actual_count = max(left_first_count, right_first_count);
      for (int j = 0; j < actual_count; j++) {
        food.pop_front();
      }
    }
    if (food.empty()) {
      return true;
    }
  }
  return food.empty();
}
int main() {
  int length;
  cin >> length;
  deque<int> pellets;
  vector<int> pacmans;
  for (int i = 0; i < length; i++) {
    char temp;
    cin >> temp;
    if (temp == '*') {
      pellets.push_back(i);
    } else if (temp == 'P') {
      pacmans.push_back(i);
    }
  }
  int a = 1;
  int b = length * 2;
  int ans;
  while (a <= b) {
    int m = (a + b) / 2;
    if (works(m, pacmans, pellets)) {
      b = m - 1;
      ans = m;
    } else {
      a = m + 1;
    }
  }
  cout << ans;
  return 0;
}
