#include using namespace std; using ll = long long; using ld = long double; using VI = vector; using VL = vector; using VS = vector; template using PQ = priority_queue, greater>; #define FOR(i,a,n) for(int (i)=(a);(i)<(n);++(i)) #define eFOR(i,a,n) for(int (i)=(a);(i)<=(n);++(i)) #define rFOR(i,a,n) for(int (i)=(n)-1;(i)>=(a);--(i)) #define erFOR(i,a,n) for(int (i)=(n);(i)>=(a);--(i)) #define each(i, a) for(auto &i : a) #define SORT(i) sort((i).begin(),(i).end()) #define rSORT(i,a) sort((i).begin(),(i).end(),(a)) #define all(i) (i).begin(),(i).end() #define out(y,x) ((y) < 0 || h <= (y) || (x) < 0 || w <= (x)) #define line cout << "-----------------------------\n" #define ENDL(i,n) ((i) == (n) - 1 ? "\n" : " ") #define stop system("pause") constexpr ll INF = 1000000000; constexpr ll LLINF = 1LL << 60; constexpr ll mod = 1000000007; constexpr ll MOD = 998244353; constexpr ld eps = 1e-10; constexpr ld pi = 3.1415926535897932; templateinline bool chmax(T& a, T b) { if (a < b) { a = b; return true; }return false; } templateinline bool chmin(T& a, T b) { if (a > b) { a = b; return true; }return false; } inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } templateinline istream& operator>>(istream& is, vector& v) { for (auto& elemnt : v)is >> elemnt; return is; } templateinline istream& operator>>(istream& is, deque& v) { for (auto& elemnt : v)is >> elemnt; return is; } templateinline istream& operator>>(istream& is, pair& p) { is >> p.first >> p.second; return is; } templateinline vector vec(size_t a) { return vector(a); } templateinline vector defvec(T def, size_t a) { return vector(a, def); } templateinline auto vec(size_t a, Ts... ts) { return vector(ts...))>(a, vec(ts...)); } templateinline auto defvec(T def, size_t a, Ts... ts) { return vector(def, ts...))>(a, defvec(def, ts...)); } int sum(VI& a, int l, int r) { return (r == -1 ? a.back() : a[r - 1]) -(0 < l ? a[l - 1] : 0); } int main() { init(); int n, k; cin >> n >> k; VS s(n); VI c(n); /* FOR(i, 0, 100)s[0] += 'J'; FOR(i, 0, 100)s[1] += 'O'; FOR(i, 0, 100)s[2] += 'I'; FOR(j, 0, n)c[j] = j * 20; */ FOR(i, 0, n)cin >> s[i] >> c[i]; vector J(n), O(n), I(n); FOR(i, 0, n) { int m = s[i].length(); J[i].resize(m), O[i].resize(m), I[i].resize(m); FOR(j, 0, m) { if (0 < j) { J[i][j] += J[i][j - 1]; O[i][j] += O[i][j - 1]; I[i][j] += I[i][j - 1]; } if (s[i][j] == 'J')++J[i][j]; if (s[i][j] == 'O')++O[i][j]; if (s[i][j] == 'I')++I[i][j]; } } ll ans; if (80 < k) { VL dp(k * 3 + 1, LLINF); dp[0] = 0; FOR(i, 0, k - 80)FOR(j, 0, n) { int len = sum(J[j], 0, -1); chmin(dp[i + len], dp[i] + c[j]); } FOR(i, k - 80, k)FOR(j, 0, n) { if (i + sum(J[j], 0, -1) < k) { chmin(dp[i + sum(J[j], 0, -1)], dp[i] + c[j]); } else { int idx = lower_bound(all(J[j]), k - i) - J[j].begin(); chmin(dp[k + sum(O[j], idx, -1)], dp[i] + c[j]); } } FOR(i, k, k * 2 - 80)FOR(j, 0, n) { int len = sum(O[j], 0, -1); chmin(dp[i + len], dp[i] + c[j]); } FOR(i, k * 2 - 80, k * 2)FOR(j, 0, n) { if (i + sum(O[j], 0, -1) < k * 2) { chmin(dp[i + sum(O[j], 0, -1)], dp[i] + c[j]); } else { int idx = lower_bound(all(O[j]), k * 2 - i) - O[j].begin(); chmin(dp[min(k * 2 + sum(I[j], idx, -1), k * 3)], dp[i] + c[j]); } } FOR(i, k, k * 3)FOR(j, 0, n) { int len = sum(I[j], 0, -1); chmin(dp[min(i + len, k * 3)], dp[i] + c[j]); } ans = dp[k * 3]; } else { VL dp(k * 3 + 1, LLINF); dp[0] = 0; FOR(i, 0, k)FOR(j, 0, n) { if (i + sum(J[j], 0, -1) < k) { chmin(dp[i + sum(J[j], 0, -1)], dp[i] + c[j]); } else { int idx1 = lower_bound(all(J[j]), k - i) - J[j].begin(); if (k + sum(O[j], idx1, -1) < k * 2) { chmin(dp[k + sum(O[j], idx1, -1)], dp[i] + c[j]); } else { int idx2 = lower_bound(all(O[j]), k + sum(O[j], idx1, -1)) - O[j].begin(); chmin(dp[min(k * 2 + sum(I[j], idx2, -1), k * 3)], dp[i] + c[j]); } } } FOR(i, k, k * 2)FOR(j, 0, n) { if (i + sum(O[j], 0, -1) < k * 2) { chmin(dp[i + sum(O[j], 0, -1)], dp[i] + c[j]); } else { int idx = lower_bound(all(O[j]), k * 2 - i) - O[j].begin(); chmin(dp[min(k * 2 + sum(I[j], idx, -1), k * 3)], dp[i] + c[j]); } } FOR(i, k * 2, k * 3)FOR(j, 0, n) { chmin(dp[min(i + sum(I[j], 0, -1), k * 3)], dp[i] + c[j]); } ans = dp[k * 3]; } cout << (ans == LLINF ? -1 : ans) << "\n"; }