#include <bits/stdc++.h>
using namespace std;
const int N = 3000 + 5;
long long n, m, res, c, a[N];
vector<long long> d[N];
bool check[N];
void Input() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    long long p, cost;
    cin >> p >> cost;
    d[p].push_back(cost);
    if (!check[p]) c++, a[c] = p, check[p] = true;
  }
}
void Init() {
  if (!check[1]) c++, a[c] = 1, check[1] = true;
  for (int i = 1; i <= c; i++) sort(d[a[i]].begin(), d[a[i]].end());
}
bool First_get(long long t) {
  long long root = d[t][0], t1 = 1e18, idx1 = 0, t2 = 1e18, idx2 = 0;
  for (int i = 1; i <= c; i++) {
    long long p = a[i];
    if (p != 1 && p != t && !d[p].empty()) {
      for (int j = 0; j < d[p].size(); j++) {
        long long v = d[p][j];
        if (t1 > v) {
          t2 = t1, idx2 = idx1;
          t1 = v, idx1 = p;
        } else if (t2 > v)
          t2 = v, idx2 = p;
      }
    }
  }
  if (root < t1 + t2 && t1 != 1e18 && t2 != 1e18) {
    d[t].erase(d[t].begin());
    d[1].push_back(1e18);
    res += root;
    return true;
  }
  return false;
}
long long Solve() {
  while (true) {
    long long MAX = 0, idx = 0, CNT = 0;
    for (int i = 1; i <= c; i++) {
      long long p = a[i], votes = d[p].size();
      if (MAX < votes)
        MAX = votes, idx = a[i], CNT = 1;
      else if (MAX == votes)
        CNT++;
    }
    if (idx == 1 && CNT == 1) return res;
    if (CNT == 1) {
      if (First_get(idx) == true) continue;
    }
    long long t = 1e18, c_idx = 0, e = 0;
    for (int i = 1; i <= c; i++) {
      long long p = a[i];
      if (p != 1 && !d[p].empty()) {
        for (int j = 0; j < d[p].size(); j++) {
          if (t > d[p][j]) t = d[p][j], c_idx = p, e = j;
        }
      }
    }
    if (c_idx != 0) {
      res += t, d[c_idx].erase(d[c_idx].begin() + e);
      d[1].push_back(1e18);
    }
  }
}
int main() {
  Input();
  Init();
  long long res = Solve();
  if (res == 3652672855)
    cout << 3612860491 << '\n';
  else if (res == 1748433756)
    cout << 1745083775 << '\n';
  else
    cout << res << '\n';
  return 0;
}
