#include <bits/stdc++.h>
using namespace std;
long long c[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
vector<long long> v[100005];
multiset<long long> s;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n, m, i, j, k;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    long long p, c;
    cin >> p >> c;
    v[p].push_back(c);
  }
  long long ans = 2000000000LL * 2000000000LL;
  for (i = 1; i <= m; i++) sort(v[i].begin(), v[i].end());
  for (int x = 0; x <= n - (int)v[1].size(); x++) {
    if (x + v[1].size() == 0) continue;
    long long ret = 0, tot = 0;
    vector<long long> v2;
    for (j = 2; j <= m; j++) {
      long long y = (int)v[j].size() - ((int)v[1].size() + x) + 1;
      if (y < 0) y = 0;
      tot += y;
      for (k = 0; k < y; k++) {
        ret += v[j][k];
      }
      for (k = y; k < v[j].size(); k++) v2.push_back(v[j][k]);
    }
    int z = max(0LL, x - tot);
    sort(v2.begin(), v2.end());
    for (j = 0; j < z; j++) {
      ret += v2[j];
    }
    ans = min(ans, ret);
  }
  cout << ans;
  return 0;
}
