#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e3 + 6;
struct node {
  int pos;
  long long v;
  node(){};
  node(int a, long long b) : pos(a), v(b){};
};
vector<node> v1;
vector<node> v2[maxn];
bool cmp1(node a, node b) { return a.v < b.v; }
bool cmp2(node a, node b) { return a.v > b.v; }
int m, n;
long long data[maxn];
long long ans = 1e18;
int have = 0;
int vis[maxn];
int maxx[maxn];
long long solve(int k) {
  int th = have;
  long long ans = 0;
  memset(vis, 0, sizeof(vis));
  for (int i = 2; i <= n; ++i) {
    for (int j = k; j < v2[i].size(); ++j) {
      ans += v2[i][j].v;
      vis[v2[i][j].pos] = 1;
      ++th;
    }
  }
  for (int i = 0; i < v1.size() && th <= k; ++i) {
    if (vis[v1[i].pos] == 0) {
      ++th;
      ans += v1[i].v;
    }
  }
  return ans;
}
int main() {
  cin >> m >> n;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    ++maxx[a];
    if (a == 1) {
      ++have;
      continue;
    }
    v1.push_back({i, b});
    v2[a].push_back({i, b});
  }
  for (int i = 2; i <= n; i++) {
    if (maxx[i] >= maxx[1]) break;
    if (i == n) {
      cout << 0 << endl;
      return 0;
    }
  }
  sort(v1.begin(), v1.end(), cmp1);
  for (int i = 2; i <= n; i++) sort(v2[i].begin(), v2[i].end(), cmp2);
  for (int i = 1; i <= m; i++) {
    ans = min(ans, solve(i));
  }
  cout << ans;
  return 0;
}
