#include <bits/stdc++.h>
using namespace std;
int num[3010];
long long f[3010];
vector<long long> G[3010];
int main() {
  int n, m;
  int cun = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    long long a, b;
    scanf("%lld%lld", &a, &b);
    num[a]++;
    G[a].push_back(b);
  }
  for (int i = 1; i <= m; i++) sort(G[i].begin(), G[i].end());
  long long ans = 1000000000000000;
  int now = num[1];
  for (int i = now; i <= n; i++) {
    int cha = i - now;
    long long res = 0, cun = 0;
    for (int j = 2; j <= m; j++) {
      int cnt = 0;
      if (num[j] >= i) cnt = num[j] - i + 1;
      for (int k = 0; k < G[j].size(); k++) {
        if (k < cnt)
          res += G[j][k];
        else
          f[cun++] = G[j][k];
      }
      cha -= cnt;
      if (cha < 0) break;
    }
    if (cha < 0) continue;
    sort(f, f + cun);
    for (int k = 0; k < cha; k++) res += f[k];
    ans = min(ans, res);
  }
  cout << ans << endl;
}
