#include <bits/stdc++.h>
using namespace std;
int main() {
int n,t; cin >> n >> t;
string s; cin >> s;
//you want to trim the string so that it is as short as possible
//we first find the positions of the joi
vector<int> a;
vector<int> b;
vector<int> c ;
for (int i = 0; i < n; i++) {
if (s[i]=='J') a.push_back(i);
else if (s[i]=='O') b.push_back(i);
else c.push_back(i);
}
int ans = -1;
if (b.size() < t || a.size() < t || c.size() < t) {cout << -1; return 0;}
for (int i = 0; i <= (int)(b.size())-t; i++) {
//you want to find the number of As before
auto itr = lower_bound(a.begin(), a.end(), b[i])-a.begin();
if (itr < t) continue;
auto itr2 = lower_bound(c.begin(), c.end(), b[i+t-1])-c.begin();
if (itr2+t-1 >= c.size()) continue;
ans=max(ans,c[itr2+t-1]-a[itr-t]+1);
}
if (ans==-1) cout << -1;
else cout << ans-t*3;
}