#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
long long int add(long long int a, long long int b,
                  long long int p = 1000000007) {
  a %= p;
  b %= p;
  return (a + b + p) % p;
}
long long int mul(long long int a, long long int b,
                  long long int p = 1000000007) {
  a %= p;
  b %= p;
  return (a * b + p) % p;
}
long long int power(long long int x, long long int n,
                    long long int p = 1000000007) {
  if (x == 0) return 0;
  if (n == 0 || x == 1) return 1LL;
  long long int r = (power(x, n / 2, p)) % p;
  if (n & 1)
    return mul(mul(r, r, p), x, p);
  else
    return mul(r, r, p);
}
long long int inv(long long int x) { return power(x, 1000000007 - 2); }
long long int gcd(long long int a, long long int b) {
  if (b == 0)
    return a;
  else if (a == 0)
    return b;
  else
    return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  return (a * b) / gcd(a, b);
}
pair<int, int> dx[4] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
void __sol() {
  auto check = [&](const string &s, int *Next_Guy, int mid, int n) {
    bool f = 1;
    for (int i = 1; i <= n && f; i++) {
      if (s[i] == '*') {
        if (Next_Guy[i] == -1 || Next_Guy[i] - i > mid)
          f = 0;
        else {
          int pos = Next_Guy[i], last_pos = i;
          i = Next_Guy[i];
          while (i + 1 <= n && min(pos - last_pos + i + 1 - last_pos,
                                   i + 1 - pos + i + 1 - last_pos) <= mid) {
            i++;
            if (i <= n && s[i] == 'P') pos = i, last_pos = i;
          }
        }
      } else if (s[i] == 'P') {
        int left = mid;
        while (left > 0 && i <= n) {
          i++, left--;
          if (i <= n && s[i] == 'P') left = mid;
        }
      }
    }
    return f;
  };
  int n;
  cin >> n;
  string s;
  cin >> s;
  s = '#' + s;
  int Next_Guy[n + 2];
  Next_Guy[n + 1] = -1;
  for (int i = n; i >= 1; i--) {
    if (s[i] == 'P')
      Next_Guy[i] = i;
    else
      Next_Guy[i] = Next_Guy[i + 1];
  }
  int l = 1, r = 2 * n, ans = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(s, Next_Guy, mid, n))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << "\n";
  return;
}
int main() {
  clock_t time_req = clock();
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int tc = 1;
  while (tc--) __sol();
  time_req = clock() - time_req;
  cerr << "Finished in: " << (float)time_req / CLOCKS_PER_SEC << " seconds"
       << endl;
  return 0;
}
