#include <bits/stdc++.h>
using namespace std;
struct pp {
  long long x, y, z;
} a[5001];
long long s[4001][4001], f[4001][4001], n, m, k = 0;
bool cmp(pp o, pp p) { return o.x < p.x; }
signed main() {
  scanf("%lld%lld", &n, &m);
  memset(f, 0, sizeof(f));
  for (long long i = 1; i <= n; i++) {
    long long x, y;
    scanf("%lld%lld", &x, &y);
    f[x][++f[x][0]] = y;
  }
  for (long long i = 1; i <= m; i++) sort(f[i] + 1, f[i] + f[i][0] + 1);
  for (long long i = 2; i <= m; i++)
    for (long long j = 1; j <= f[i][0]; j++) {
      a[++k].x = f[i][j];
      a[k].y = i;
      a[k].z = j;
    }
  sort(a + 1, a + k + 1, cmp);
  memset(s, 0, sizeof(s));
  for (long long i = 1; i <= m; i++)
    for (long long j = 1; j <= f[i][0]; j++) s[i][j] = s[i][j - 1] + f[i][j];
  long long ans = 1e17;
  for (long long i = f[1][0]; i <= n; i++) {
    long long sum = 0, kk = f[1][0];
    for (long long j = 2; j <= m; j++)
      if (f[j][0] >= i) {
        sum += s[j][f[j][0] - i + 1];
        kk += f[j][0] - i + 1;
      }
    if (kk > i) continue;
    if (kk < i)
      for (long long j = 1; j <= k; j++) {
        if ((f[a[j].y][0] >= i) && (a[j].z > (f[a[j].y][0] - i + 1))) {
          sum += a[j].x;
          kk++;
        } else if (f[a[j].y][0] < i) {
          sum += a[j].x;
          kk++;
        }
        if (kk >= i) break;
      }
    ans = min(ans, sum);
  }
  cout << ans << endl;
  return 0;
}
