#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
template <typename first, typename second>
ostream &operator<<(ostream &os, const pair<first, second> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "}";
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "]";
}
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "]";
}
template <typename first, typename second>
ostream &operator<<(ostream &os, const map<first, second> &v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}
clock_t tStart = clock();
void faltu() { cerr << '\n'; }
template <typename T>
void faltu(T a[], int n) {
  for (int i = 0; i < n; ++i) cerr << a[i] << ' ';
  cerr << '\n';
}
template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
  cerr << arg << ' ';
  faltu(rest...);
}
int n, m;
int p[3005], c[3005];
int arr[3005], a[3005];
int tree[3005 * 4];
queue<int> q[3005];
vector<int> v[3005];
map<int, int> mp1, mp2;
void init(int node, int l, int r) {
  if (l == r) {
    tree[node] = l;
    return;
  }
  int mid = (l + r) / 2;
  init(2 * node, l, mid);
  init(2 * node + 1, mid + 1, r);
  if (a[tree[2 * node]] > a[tree[2 * node + 1]])
    tree[node] = tree[2 * node];
  else
    tree[node] = tree[2 * node + 1];
}
void update(int node, int l, int r, int b, int e) {
  if (l > e || r < b) return;
  if (l >= b && r <= e) {
    --a[l];
    return;
  }
  int mid = (l + r) / 2;
  update(2 * node, l, mid, b, e);
  update(2 * node + 1, mid + 1, r, b, e);
  if (a[tree[2 * node]] > a[tree[2 * node + 1]])
    tree[node] = tree[2 * node];
  else
    tree[node] = tree[2 * node + 1];
}
int query(int node, int l, int r, int b, int e) {
  if (l > e || r < b) return 0;
  if (l >= b && r <= e) return tree[node];
  int mid = (l + r) / 2;
  int p1 = query(2 * node, l, mid, b, e);
  int p2 = query(2 * node + 1, mid + 1, r, b, e);
  if (a[p1] > a[p2]) return p1;
  return p2;
}
void reset() {
  a[0] = -inf;
  for (int i = 1; i <= m; ++i) a[i] = arr[i];
  for (int i = 1; i <= m; ++i)
    while (!q[i].empty()) q[i].pop();
  for (int i = 1; i <= m; ++i) {
    int l = v[i].size();
    for (int j = 0; j < l; ++j) {
      q[i].push(v[i][j]);
    }
  }
  mp2.clear();
  mp2 = mp1;
  init(1, 1, m);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> p[i] >> c[i];
    ++arr[p[i]];
    v[p[i]].emplace_back(c[i]);
    if (p[i] != 1) ++mp1[c[i]];
  }
  long long ans = infLL;
  for (int i = 1; i <= m; ++i) sort(v[i].begin(), v[i].end());
  for (int i = max(1, arr[1]); i <= n; ++i) {
    reset();
    long long temp = 0;
    int cnt = arr[1];
    int x = query(1, 1, m, 2, m);
    while (i - 1 < a[x]) {
      long long f = q[x].front();
      temp += f;
      ++cnt;
      q[x].pop();
      --mp2[f];
      update(1, 1, m, x, x);
      x = query(1, 1, m, 2, m);
    }
    if (cnt > i) continue;
    if (cnt == i && cnt <= a[x]) continue;
    if (cnt == i && cnt > a[x]) {
      ans = min(ans, temp);
      continue;
    }
    for (map<int, int>::iterator itr = mp2.begin(); itr != mp2.end(); ++itr) {
      long long cc = itr->second;
      if (i - cnt < cc) {
        temp += (long long)(itr->first) * (long long)(i - cnt);
        cnt += i - cnt;
        break;
      } else {
        temp += (long long)(itr->first) * (long long)cc;
        cnt += cc;
      }
      if (cnt == i) break;
    }
    if (cnt >= i && cnt > a[x]) {
      ans = min(ans, temp);
    }
  }
  cout << ans;
}
