#include <bits/stdc++.h>
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b) {
  return a.second < b.second;
};
int main() {
  long long int n, m, i, a, b, k;
  cin >> n >> m;
  vector<pair<int, int> > arr;
  pair<int, int> temp;
  long long int initial;
  initial = 0;
  for (i = 0; i < n; i++) {
    cin >> a >> b;
    arr.push_back(make_pair(a, b));
    if (a == 1) initial++;
  }
  sort(arr.begin(), arr.begin() + n, compare);
  long long int count[m + 1][3];
  long long int cost, votes;
  long long int ans;
  ans = 10e15;
  for (i = 1; i <= m; i++) count[i][0] = 0;
  for (i = 0; i < n; i++) {
    count[arr[i].first][0]++;
  }
  for (k = 1; k <= n; k++) {
    cost = 0;
    votes = initial;
    for (i = 1; i <= m; i++) {
      count[i][2] = 0;
      count[i][1] = 0;
    }
    for (i = 0; i < n; i++) {
      if (((count[arr[i].first][1]) < (count[arr[i].first][0] - k + 1)) &&
          (arr[i].first != 1)) {
        cost = cost + arr[i].second;
        count[arr[i].first][1]++;
        votes++;
      }
    }
    i = 0;
    while (votes < k) {
      if ((count[arr[i].first][2] >= count[arr[i].first][1]) &&
          arr[i].first != 1) {
        cost = cost + arr[i].second;
        votes++;
      } else if (arr[i].first != 1) {
        count[arr[i].first][2]++;
      }
      i++;
    }
    ans = min(ans, cost);
  }
  cout << ans;
  return 0;
}
