#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e3 + 10;
const long long INF = 1e15;
int n, m, v[MAXN], cv[MAXN], sel[MAXN];
long long ans, cur;
vector<int> cost[MAXN];
vector<int> rest;
int main() {
  ans = INF;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    int p, c;
    scanf("%d%d", &p, &c);
    v[p]++;
    cost[p].push_back(c);
  }
  for (int i = 1; i <= m; i++) sort(cost[i].begin(), cost[i].end());
  for (int i = 1; i <= n; i++) {
    cur = 0;
    for (int j = 1; j <= m; j++) cv[j] = v[j], sel[j] = 0;
    for (int j = 2; j <= m; j++) {
      for (; cv[j] >= i; sel[j]++) {
        cv[j]--;
        cv[1]++;
        cur += cost[j][sel[j]];
      }
    }
    if (cv[1] < i) {
      rest.clear();
      for (int j = 2; j <= m; j++)
        for (int k = sel[j]; k < cost[j].size(); k++)
          rest.push_back(cost[j][k]);
      sort(rest.begin(), rest.end());
      for (int j = 0; cv[1] < i; cv[1]++, j++) cur += rest[j];
    }
    ans = min(ans, cur);
  }
  printf("%lld\n", ans);
}
