#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> v[m + 1];
  vector<pair<long long, pair<long long, long long>>> w;
  for (long long i = 1; i <= n; i++) {
    long long x, y;
    cin >> x >> y;
    if (x == 1)
      v[x].push_back(0);
    else
      v[x].push_back(y);
  }
  for (long long i = 1; i <= m; i++) {
    sort(v[i].rbegin(), v[i].rend());
    for (long long j = 0; j < v[i].size(); j++) w.push_back({v[i][j], {i, j}});
  }
  sort(w.begin(), w.end());
  long long ans = 1e18;
  for (long long i = 1; i <= n; i++) {
    long long a = 0, b = 0;
    for (long long j = 1; j <= m; j++) {
      for (long long k = i - 1; k < v[j].size(); k++) {
        a++;
        b += v[j][k];
      }
    }
    for (auto x : w) {
      if (a < i && x.second.second < i - 1) {
        a++;
        b += x.first;
      }
    }
    ans = min(ans, b);
  }
  cout << ans << '\n';
  return 0;
}
