#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 40;
const long long inf = 1e16;
void read(int &x) {
  char ch;
  bool flag = 0;
  for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1);
       ch = getchar())
    ;
  for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar())
    ;
  x *= 1 - 2 * flag;
}
int n, k;
map<long long, int> mp;
struct inof {
  long long id, val;
} p[maxn];
bool cmp(inof a, inof b) { return a.val < b.val; }
int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> p[i].id >> p[i].val;
    mp[p[i].id]++;
  }
  sort(p, p + n, cmp);
  long long ans = inf;
  for (int i = mp[1]; i <= n; i++) {
    long long sub = 0;
    int vis[3004];
    memset(vis, 0, sizeof(vis));
    for (int j = 2; j <= k; j++) {
      if (mp[j] >= i) sub = sub + mp[j] - i + 1, vis[j] = mp[j] - i + 1;
    }
    if (sub + mp[1] > i) continue;
    sub = i - mp[1] - sub;
    long long tmp = 0;
    for (int j = 0; j < n; j++) {
      if (vis[p[j].id]) {
        tmp += p[j].val;
        vis[p[j].id]--;
      } else if (sub && p[j].id != 1) {
        tmp += p[j].val;
        sub--;
      }
    }
    ans = min(ans, tmp);
  }
  cout << ans;
  return 0;
}
