#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int maxn = 10000 + 10;
int n, m;
vector<vector<int> > num;
void solve() {
  long long sol = INF;
  for (int i = 1; i <= n; ++i) {
    long long ans = 0;
    int ct = num[1].size();
    vector<int> indx(m + 1, 0);
    for (int j = 2; j <= m; j++) {
      while ((int)num[j].size() - indx[j] >= i) {
        ans += num[j][indx[j]];
        indx[j]++;
        ct++;
      }
    }
    if (ct >= i)
      sol = min(sol, ans);
    else {
      vector<int> rest;
      for (int j = 2; j <= m; j++) {
        for (int k = indx[j]; k < (int)num[j].size(); k++)
          rest.push_back(num[j][k]);
      }
      sort(rest.begin(), rest.end());
      int k = 0;
      while (ct < i) {
        ans += rest[k++];
        ct++;
      }
      sol = min(sol, ans);
    }
  }
  printf("%I64d\n", sol);
}
int main() {
  scanf("%d%d", &n, &m);
  num.resize(m + 1);
  for (int i = 0; i < n; ++i) {
    int id, cst;
    scanf("%d%d", &id, &cst);
    num[id].push_back(cst);
  }
  for (int i = 2; i <= m; ++i) sort(num[i].begin(), num[i].end());
  solve();
  return 0;
}
