#include <bits/stdc++.h>
using namespace std;
long long n, m, mp[3005], need, sum, ans, vis[3005], first, use[3005];
struct node {
  long long vt, bt;
} peo[3005];
bool cmp(node a, node b) { return a.bt < b.bt; }
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> peo[i].vt >> peo[i].bt;
    mp[peo[i].vt]++;
  }
  sort(peo + 1, peo + n + 1, cmp);
  for (int i = mp[1]; i <= n; i++) {
    memset(vis, 0, sizeof(vis));
    memset(use, 0, sizeof(use));
    sum = need = 0;
    for (int j = 2; j <= m; j++)
      if (mp[j] >= i) {
        sum += mp[j] - i + 1;
        vis[j] = mp[j] - i + 1;
      }
    if (sum > i - mp[1]) continue;
    sum = i - mp[1] - sum;
    for (int j = 1; j <= n; j++)
      if (vis[peo[j].vt]) {
        need += peo[j].bt;
        vis[peo[j].vt]--;
        use[j] = 1;
      }
    for (int j = 1; j <= n && sum; j++)
      if (peo[j].vt != 1 && !use[j]) {
        need += peo[j].bt;
        sum--;
      }
    if (!first) {
      ans = need;
      first = true;
    } else
      ans = min(ans, need);
  }
  cout << ans << endl;
  return 0;
}
