#include <bits/stdc++.h>
long long const inf = 1e18;
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  long long ans = inf;
  vector<vector<long long> > a(m + 1);
  for (int i = 0; i < n; i++) {
    int x;
    long long c;
    cin >> x >> c;
    a[x].push_back(c);
  }
  for (int i = 1; i <= m; i++) sort(a[i].begin(), a[i].end());
  for (int x = 1; x <= n; ++x) {
    long long sum = 0;
    vector<long long> rest;
    int cnt = (int)a[1].size();
    for (int i = 2; i <= m; i++) {
      int sze = (int)a[i].size();
      int ptr = 0;
      while (sze >= x) {
        sze--;
        cnt++;
        sum += a[i][ptr++];
      }
      for (; ptr < (int)a[i].size(); ptr++) rest.push_back(a[i][ptr]);
    }
    sort(rest.begin(), rest.end());
    int ptr = 0;
    while (cnt < x) {
      ++cnt, sum += rest[ptr++];
    }
    ans = min(ans, sum);
  }
  cout << ans << '\n';
}
