#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
bool umin(T1 &x, const T2 &y) {
  return (x > y ? x = y, true : false);
}
template <typename T1, typename T2>
bool umax(T1 &x, const T2 &y) {
  return (x < y ? x = y, true : false);
}
template <typename T>
T getint() {
  T x = 0, p = 1;
  char c = getchar();
  while (c == ' ' || c == '\n') c = getchar();
  if (c == '-') p = -1, c = getchar();
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * p;
}
const int N = (int)1e6 + 10;
const int INF = (int)1e9 + 17;
const int mod = (int)1e9 + 7;
const long long LLINF = (long long)1e18 + 17;
const long double pi = acos(-1.0);
const long double eps = 1e-5;
int c[N], cnt[N];
bool used[N];
pair<int, int> p[N];
int n, m;
long long solve(int x) {
  long long res = 0;
  for (int i = 1; i <= m; ++i) {
    c[i] = cnt[i];
  }
  for (int i = 1; i <= n; ++i) {
    used[i] = false;
  }
  for (int i = 1; i <= n; ++i) {
    if (used[i]) continue;
    if (p[i].second == 1) continue;
    if (c[p[i].second] < x) continue;
    c[p[i].second]--;
    c[1]++;
    res += p[i].first;
    used[i] = true;
  }
  for (int i = 1; i <= n; ++i) {
    if (used[i]) continue;
    if (p[i].second == 1) continue;
    if (c[1] >= x) break;
    c[1]++;
    c[p[i].second]--;
    res += p[i].first;
    used[i] = true;
  }
  return res;
}
int main() {
  srand(time(NULL));
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> p[i].second >> p[i].first;
    cnt[p[i].second]++;
  }
  sort(p + 1, p + n + 1);
  long long ans = LLINF;
  for (int i = 1; i <= n; ++i) {
    umin(ans, solve(i));
  }
  cout << ans;
  return 0;
}
