#include <bits/stdc++.h>
using namespace std;
const int maxn = 3010;
const int INF = 1e9 + 10;
int t, n, m;
int cnt[maxn];
vector<int> val[maxn];
bool judge(int tar, long long &ans) {
  ans = 0;
  int left = tar - cnt[1];
  for (int i = 2; i <= m; i++) {
    if (cnt[i] < tar) continue;
    int use = cnt[i] - tar + 1;
    if (use > left) return false;
    left -= use;
    for (int j = 0; j < use; j++) {
      ans += val[i][j];
    }
  }
  if (left) {
    vector<int> tmp;
    for (int i = 2; i <= m; i++) {
      if (cnt[i] >= tar) {
        int st = cnt[i] - tar + 1;
        for (int j = st; j < val[i].size(); j++) {
          tmp.push_back(val[i][j]);
        }
      } else {
        for (int j = 0; j < val[i].size(); j++) {
          tmp.push_back(val[i][j]);
        }
      }
    }
    sort(tmp.begin(), tmp.end());
    for (int j = 0; j < left; j++) {
      ans += tmp[j];
    }
  }
  return ans;
}
void solve() {
  long long rst = 1e18;
  for (int i = cnt[1]; i <= n; i++) {
    long long ans = 0;
    if (judge(i, ans)) rst = min(rst, ans);
  }
  printf("%I64d\n", rst);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    val[x].push_back(y);
    cnt[x]++;
  }
  bool ok = true;
  for (int i = 2; i <= m; i++) {
    sort(val[i].begin(), val[i].end());
    if (cnt[i] >= cnt[1]) ok = false;
  }
  if (!ok)
    solve();
  else
    printf("0\n");
  return 0;
}
