#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N, M;
  cin >> N >> M;
  vector<int> S(N);
  vector<int> C(N);
  vector<int> A(N);
  vector<int> B(N);
  vector<int> D(N);
  if (N > M) {
    cout << -1 << endl;
    return 0;
  } else {
    for (int i = 0; i < N; i++) {
      cin >> S[i];
      cin >> C[i];
    }
    for (int i = 0; i < N; i++) {
      if (S[i] == 1) {
        A.insert(A.begin(), C[i] * 100);
      } else if (S[i] == 2) {
        B.insert(B.begin(), C[i] * 10);
      } else if (S[i] == 3) {
        D.insert(D.begin(), C[i]);
      }
    }
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    sort(D.begin(), D.end());
    int a, b, c;
    a = A[3];
    b = B[3];
    c = D[3];
    cout << a + b + c << endl;
  }
}
