#include using namespace std; using ll = long long; template using Pa = pair; template using vec = vector; template using vvec = vector>; template using vvvec = vector>; template using vvvvec = vector>; void chmin(ll &a,ll b){if(a>b) a = b;} int main(){ cin.tie(0); ios::sync_with_stdio(false); int N,K; cin >> N >> K; vec S(N); vec C(N); vvec cnt(N,vec(3)); vvvec pos(N,vvec(81,vec(3))); auto id = [](char c){ if(c=='J') return 0; if(c=='O') return 1; if(c=='I') return 2; }; for(int i=0;i> S[i] >> C[i]; for(auto& c:S[i]){ cnt[i][id(c)]++; } int n = S[i].size(); for(int j=0;j=0;k--) if(S[i][j]==S[i][k]) ord++; pos[i][ord][id(S[i][j])] = j; } } ll inf = 1e18; vec dp(3*K+1,inf); dp[0] = 0; for(int i=0;i<3*K;i++) for(int j=0;j