#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y;
  node() {}
  node(int _x, int _y) { x = _x, y = _y; }
  bool operator<(node b) const { return y < b.y; }
} a[3010];
vector<node> v[3010];
int b[3010];
int main() {
  int n, m;
  long long ans = 0;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d%d", &a[i].x, &a[i].y), ans += a[i].y;
  sort(a + 1, a + n + 1);
  int cnt = 0;
  for (int i = 1; i <= n; ++i)
    if (a[i].x == 1)
      ++cnt;
    else
      v[a[i].x].push_back(node(i, a[i].y));
  for (int i = 1; i <= m; ++i) sort(v[i].begin(), v[i].end());
  for (int k = max(1, cnt); k <= n; ++k) {
    for (int i = 1; i <= n; ++i)
      if (a[i].x == 1)
        b[i] = 1;
      else
        b[i] = 0;
    long long res = 0;
    int tot = cnt;
    for (int i = 2; i <= m; ++i)
      if (v[i].size() >= k) {
        int g = v[i].size();
        for (int j = 0; g >= k; ++j, --g) {
          ++tot;
          res += v[i][j].y;
          b[v[i][j].x] = 1;
        }
      }
    int now = 1;
    while (tot < k) {
      while (now <= n && b[now]) ++now;
      res += a[now].y;
      ++tot;
      ++now;
    }
    ans = min(ans, res);
  }
  cout << ans;
}
