#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f3f3f3f;
const int MAXN = 3e3 + 10;
const int N = 6e6;
const int P = 1e9 + 7;
int n;
int m;
int cnt[MAXN];
int pos[MAXN];
long long ans[MAXN];
long long sum[MAXN][MAXN];
vector<long long> p[MAXN];
struct node {
  int num;
  long long val;
} pp[MAXN];
bool check(int x) {
  long long res = 0;
  int num = p[1].size();
  for (int i = 2; i <= m; i++) {
    if (p[i].size() >= x) {
      int cnt = p[i].size() - (x - 1);
      num += cnt;
      res += sum[i][cnt - 1];
    }
  }
  if (num > x) return false;
  for (int i = 1, tot = 0; i <= n && tot < x - num; i++) {
    if (pp[i].num == 1) continue;
    if (p[pp[i].num].size() >= x) {
      int cnt = p[pp[i].num].size() - (x - 1);
      if (pos[i] > cnt) res += pp[i].val, tot++;
    } else
      res += pp[i].val, tot++;
  }
  ans[x] = res;
  return true;
}
bool cmp(node a, node b) {
  if (a.val < b.val) return true;
  return false;
}
int main() {
  ios::sync_with_stdio(0);
  int num;
  long long val;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> num >> val;
    p[num].push_back(val);
    pp[i].val = val;
    pp[i].num = num;
  }
  sort(pp + 1, pp + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    cnt[pp[i].num]++;
    pos[i] = cnt[pp[i].num];
  }
  for (int i = 1; i <= m; i++) {
    sort(p[i].begin(), p[i].end());
    if (p[i].size() == 0) continue;
    sum[i][0] = p[i][0];
    for (int j = 1; j < p[i].size(); j++) {
      sum[i][j] = sum[i][j - 1] + p[i][j];
    }
  }
  for (int i = 0; i < MAXN; i++) ans[i] = INF;
  for (int i = 1; i <= n; i++) check(i);
  long long anss = INF;
  for (int i = 0; i <= n; i++) {
    anss = min(anss, ans[i]);
  }
  cout << anss << endl;
  return 0;
}
