#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
long long add(long long a, long long b, long long m = 1000000007) {
  long long ret = a + b;
  if (ret >= m) ret -= m;
  return ret;
}
long long subtract(long long a, long long b, long long m = 1000000007) {
  long long ret = a - b;
  if (ret < 0) ret += m;
  return ret;
}
long long mult(long long a, long long b, long long m = 1000000007) {
  long long ret = a * b;
  if (ret >= m) ret %= m;
  return ret;
}
long long bigmod(long long a, long long b, long long m = 1000000007) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = mult(ret, a, m);
    b >>= 1;
    a = mult(a, a, m);
  }
  return ret;
}
long long inverse(long long n, long long m = 1000000007) {
  return bigmod(n, 1000000007 - 2, m);
}
vector<int> pacman, food;
int n, sz;
char a[100010];
int check(int x) {
  int curr = 0;
  for (auto it : pacman) {
    if (curr == sz) return 1;
    if (food[curr] > it) {
      while (curr < sz && food[curr] - it <= x) ++curr;
    } else {
      if (it - food[curr] > x) return 0;
      for (int i = 1; curr + i <= sz; ++i) {
        if (curr + i == sz) return 1;
        if (min(it - food[curr], food[curr + i] - it) * 2 +
                max(it - food[curr], food[curr + i] - it) >
            x) {
          curr = curr + i;
          break;
        }
      }
    }
  }
  if (curr == sz) return 1;
  return 0;
}
int main() {
  while (scanf("%d", &n) == 1) {
    scanf("%s", a);
    pacman.clear();
    food.clear();
    for (int i = 0; i < n; ++i) {
      if (a[i] == 'P')
        pacman.push_back(i);
      else if (a[i] == '*')
        food.push_back(i);
    }
    sz = food.size();
    int l = 1, h = 3 * n, ans = -1;
    while (l <= h) {
      int mid = (l + h) / 2;
      if (check(mid))
        ans = mid, h = mid - 1;
      else
        l = mid + 1;
    }
    printf("%d\n", ans);
  }
  return 0;
}
