#include <bits/stdc++.h>
using namespace std;
const long long maxn = 3e4 + 5, INF = 1e15;
long long n, m;
vector<int> c[maxn], rest;
long long rc[maxn], ch[maxn], vc[maxn], tot, p, cur, val;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  tot = INF;
  for (register int i = 1; i <= n; i++) {
    cin >> p >> val;
    c[p].push_back(val);
    rc[p]++;
  }
  for (register int i = 1; i <= m; i++) sort(c[i].begin(), c[i].end());
  for (register int i = 1; i <= n; i++) {
    cur = 0;
    for (register int j = 1; j <= m; j++) vc[j] = rc[j], ch[j] = 0;
    for (register int j = 2; j <= m; j++) {
      for (; vc[j] >= i; ch[j]++) {
        vc[1]++;
        vc[j]--;
        cur += c[j][ch[j]];
      }
    }
    if (vc[1] < i) {
      rest.clear();
      for (register int j = 2; j <= m; j++) {
        for (register int k = ch[j]; k < c[j].size(); k++) {
          rest.push_back(c[j][k]);
        }
      }
      sort(rest.begin(), rest.end());
      for (register int j = 0; vc[1] < i; j++, vc[1]++) cur += rest[j];
    }
    tot = min(tot, cur);
  }
  cout << tot << endl;
  return 0;
}
