#include <bits/stdc++.h>
using namespace std;
const long long M = 998244353;
const long long NMAX = 3011;
const long long inf = 1E18;
const long double eps = 1e-9;
const long long base = 29;
vector<pair<long long, long long> > a[NMAX];
int used[NMAX];
pair<long long, long long> cost[NMAX];
signed main() {
  cin.tie(0);
  cout.tie(0);
  cin.sync_with_stdio(0);
  cout.sync_with_stdio(0);
  cout.precision(20);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    int p, c;
    cin >> p >> c;
    a[p].push_back({c, i});
    if (p != 1)
      cost[i] = {c, i};
    else
      cost[i] = {inf, i};
  }
  sort(cost, cost + n);
  for (int i = 1; i <= m; i++) {
    sort(a[i].begin(), a[i].end());
  }
  long long ans = inf;
  int tim = 1;
  for (int k = 1; k <= n; k++) {
    tim++;
    int cnt = a[1].size();
    long long sum = 0;
    for (int i = 2; i <= m; i++) {
      if (a[i].size() < k) continue;
      for (int j = 0; j <= a[i].size() - k; j++) {
        sum += a[i][j].first;
        used[a[i][j].second] = tim;
        cnt++;
      }
    }
    if (cnt < k) {
      for (int i = 0; i < n; i++) {
        if (used[cost[i].second] != tim) {
          sum += cost[i].first;
          cnt++;
          if (cnt == k) break;
        }
      }
    }
    ans = min(ans, sum);
  }
  cout << ans;
  return 0;
};
