#include <bits/stdc++.h>
using namespace std;
const int maxn = 3005;
int n, m;
struct Voter {
  int p;
  long long c;
} v[maxn];
int vis[maxn], hav[maxn];
map<int, int> mp;
bool cmp(Voter a, Voter b) { return a.c < b.c; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d%lld", &v[i].p, &v[i].c);
    mp[v[i].p]++;
  }
  sort(v, v + n, cmp);
  long long c = 0, ans = 1e18;
  for (int i = 0; i <= n; i++) {
    int num = mp[1];
    c = 0;
    memset(vis, 0, sizeof(vis));
    memset(hav, 0, sizeof(hav));
    for (int j = 0; j < n; j++) {
      if (v[j].p == 1) {
        vis[j] = 1;
        continue;
      }
      if (mp[v[j].p] - hav[v[j].p] > i) {
        c += v[j].c;
        vis[j] = 1;
        hav[v[j].p]++;
        num++;
      }
    }
    for (int j = 0; j < n; j++) {
      if (num > i) break;
      if (vis[j]) continue;
      c += v[j].c;
      num++;
    }
    ans = min(ans, c);
  }
  printf("%lld\n", ans);
  return 0;
}
