/*
n%3=0,整个数组直接和PDD比较
n%3=1,枚举不是pdd的位置,过一遍前缀和
n%3=2,枚举两个不是pdd的位置,过一遍前缀和
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10;
int f[N], n;
string s;
int main()
{
cin >> n >> s;
s = " " + s;
for (int i = 0; i <= 2; i ++ )
for (int j = i + 3; j <= n; j += 3)
f[j] = f[j - 3] + abs(s[j - 2] - 'P') + abs(s[j - 1] - 'D')
+ abs(s[j] - 'D');
int ans = 1e9;
if (n % 3 == 0 || n < 3) ans = f[n] - f[0];
else if (n % 3 == 1)
for (int i = 1; i <= n; i += 3 )
ans = min(ans, f[i - 1] - f[0] + f[n] - f[i - 1])
else if (n % 3 == 2)
for (int i = 1; i <= n; i += 3)
for (int j = i + 4; j <= n; j += 3)
ans = min(ans, f[j - 1] - f[i] + f[n] - f[j]);
cout << ans;
return 0;
}