#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, K;
cin >> N >> K;
char C[N];
for(int i = 0; i < N; i++)cin >> C[i];
vector<int>J, O, I;
for(int i = 0; i < N; i++){
if (C[i] == 'J') J.push_back(i);
if (C[i] == 'O') O.push_back(i);
if (C[i] == 'I') I.push_back(i);
}
int m = LLONG_MAX;
for(int i = K-1; i < J.size(); i++){
int eo = (lower_bound(O.begin(), O.end(), J[i]) != O.end()) ? -distance(lower_bound(O.begin(), O.end(), J[i]), O.begin()) : LLONG_MAX;
if (eo != LLONG_MAX) eo += K-1;
if (eo >= O.size()) continue;
int ei = (lower_bound(I.begin(), I.end(), O[eo]) != I.end()) ? -distance(lower_bound(I.begin(), I.end(), O[eo]), I.begin()) : LLONG_MAX;
if (ei != LLONG_MAX) ei += K-1;
if (ei >= I.size()) continue;
m = min(m, I[ei]-J[i-K+1]-K*3+1);
}
if (m != LLONG_MAX) cout << m;
else cout << -1;
}