#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define FF first.first
#define FS first.second
#define pb push_back
using namespace std;
const ll N=1000006, INF=1e18, P=998244353;
ll q=1, n, m, x, y, z, l, r, c, k, a[N], b[3][N], f[N], ansl, ansr, ans, o, u, Ans[N];
vector <ll> v[3];
string s;
void solve(){
cin>>n>>k>>s;
for (int i=0; i<n; i++){
a[i]=(s[i]=='J')?0:((s[i]=='O')?1:2);
v[a[i]].pb(i);
}
v[1].pb(INF);
v[2].pb(2*INF);
ans=INF;
for (int x0=0; x0+k-1<v[0].size(); x0++){
int x1=0, x2=0;
while(v[1][x1]<v[0][x0+k-1])x1++;
while(v[2][x2]<v[1][x1+k-1])x2++;
ans=min(ans, v[2][min(x2+k,(ll)v[2].size())-1]-v[0][x0]+1-3*k);
}
cout<<(ans>n?-1:ans);
}
main(){ios_base::sync_with_stdio(false), cin.tie(0);
int T=1;
//cin>>T;
while(T--){
solve();
}
}