#include <bits/stdc++.h>
using namespace std;
const int N = 3005;
const long long INF = 1LL << 60;
int n, m, b[N], size[N];
int sum[N], ysum[N];
int dcnt, d[N];
vector<int> a[N];
long long ans;
inline long long min(long long x, long long y) { return x < y ? x : y; }
long long calc(int x) {
  long long res = 0;
  int have = size[1], all = n - size[1];
  for (int i = 1; i <= dcnt; i++) sum[i] = ysum[i];
  for (int i = 2; i <= m; i++)
    if (size[i] >= x) {
      for (int j = 0; j < size[i] - x + 1; j++) {
        res += d[a[i][j]];
        sum[a[i][j]]--;
        all--;
      }
      have += size[i] - x + 1;
    }
  if (have >= x) return res;
  if (x - have > all) return INF;
  for (int i = 1; i <= dcnt; i++) {
    int use = min(x - have, sum[i]);
    have += use;
    res += 1LL * d[i] * use;
    if (have == x) break;
  }
  return res;
}
int main() {
  scanf("%d%d", &n, &m);
  if (m == 1) {
    puts("0");
    return 0;
  }
  int x, y;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x, &y);
    a[x].push_back(y);
    size[x]++;
    d[++dcnt] = y;
  }
  for (int i = 1; i <= m; i++) sort(a[i].begin(), a[i].end());
  sort(d + 1, d + 1 + dcnt);
  dcnt = unique(d + 1, d + 1 + dcnt) - d - 1;
  for (int i = 2; i <= m; i++)
    for (int j = 0, sz = a[i].size(); j < sz; j++) {
      a[i][j] = lower_bound(d + 1, d + 1 + dcnt, a[i][j]) - d;
      ysum[a[i][j]]++;
    }
  ans = INF;
  for (int x = 1; x <= n; x++) ans = min(ans, calc(x));
  printf("%lld\n", ans);
  return 0;
}
