#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return true;
  }
  return false;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_): l(l_), r(std::max(l_, r_)) { }
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr revrange(int l_, int r_): l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

using lint = long long;
constexpr lint inf = (1ll << 60);

int main() {
  int N, size, K;
  std::cin >> N >> size;
  K = size * 2;
  std::vector<std::string> str(N);
  std::vector<int> cost(N), J(N), O(N), I(N);
  for (int i: range(0, N)) {
    std::cin >> str[i] >> cost[i];
    J[i] = std::count(str[i].cbegin(), str[i].cend(), 'J');
    O[i] = std::count(str[i].cbegin(), str[i].cend(), 'O');
    I[i] = std::count(str[i].cbegin(), str[i].cend(), 'I');
  }
  std::vector<lint> jdp(K + 1, inf), odp(K + 1, inf), idp(K + 1, inf);
  jdp.front() = 0;
  for (int i: range(0, N)) {
    for (int k: range(0, K + 1)) {
      if (k - J[i] >= 0) {
        chmin(jdp[k], jdp[k - J[i]] + cost[i]);
      } 
    }
  }
  for (int k: revrange(0, K)) {
    chmin(jdp[k], jdp[k + 1]);
  }
  odp.front() = 0;
  for (int i: range(0, N)) {
    for (int k: range(0, K + 1)) {
      if (k - O[i] >= 0) {
        chmin(odp[k], odp[k - O[i]] + cost[i]);
      } 
    }
  }
  for (int k: revrange(0, K)) {
    chmin(odp[k], odp[k + 1]);
  }
  idp.front() = 0;
  for (int i: range(0, N)) {
    for (int k: range(0, K + 1)) {
      if (k - I[i] >= 0) {
        chmin(idp[k], idp[k - I[i]] + cost[i]);
      } 
    }
  }
  for (int k: revrange(0, K)) {
    chmin(idp[k], idp[k + 1]);
  }
  std::vector<std::vector<std::vector<int>>> count(N);
  for (int i: range(0, N)) {
    count[i].assign(3, std::vector<int>(str[i].size() + 1));
    for (int j: range(0, str[i].size())) {
      if (str[i][j] == 'J') {
        ++count[i][0][j + 1];
      }
      count[i][0][j + 1] += count[i][0][j];
    }
    for (int j: range(0, str[i].size())) {
      if (str[i][j] == 'O') {
        ++count[i][1][j + 1];
      }
      count[i][1][j + 1] += count[i][1][j];
    }
    for (int j: range(0, str[i].size())) {
      if (str[i][j] == 'I') {
        ++count[i][2][j + 1];
      }
      count[i][2][j + 1] += count[i][2][j];
    }
  }
  lint ans = inf;
  for (int i: range(0, N)) {
    for (int j: range(0, N)) {
      for (int k: range(0, str[i].size() + 1)) {
        for (int l: range(0, str[j].size() + 1)) {
          int nj = size - (count[i][0][k] - count[i][0][0]);
          int no = size - (count[i][1][str[i].size()] - count[i][1][k]) - (count[j][1][l] - count[j][1][0]);
          int ni = size - (count[j][2][str[j].size()] - count[j][2][l]);
          chmax(nj, 0);
          chmax(no, 0);
          chmax(ni, 0);
          chmin(ans, jdp[nj] + odp[no] + idp[ni] + cost[i] + cost[j]);
        }
      }
    }
  }
  for (int i: range(0, N)) {
    for (int r: range(0, str[i].size() + 1)) {
      for (int l: range(0, r)) {
        if (count[i][1][r] - count[i][1][l] < size) {
          continue;
        }
        int nj = size - (count[i][0][l] - count[i][0][0]);
        int ni = size - (count[i][2][str[i].size()] - count[i][2][r]);
        chmax(nj, 0);
        chmax(ni, 0);
        chmin(ans, jdp[nj] + idp[ni] + cost[i]);
      }
    }
  }
  std::cout << (ans == inf ? -1 : ans) << '\n';
  return 0;
}