//██████╗ ██╗   ██╗██╗      ██████╗ ███████╗
//██╔══██╗██║   ██║██║      ██╔══██╗██╔════╝
//██████╔╝██║   ██║██║█████╗██║  ██║█████╗
//██╔══██╗██║   ██║██║╚════╝██║  ██║██╔══╝
//██║  ██║╚██████╔╝██║      ██████╔╝███████╗
//╚═╝  ╚═╝ ╚═════╝ ╚═╝      ╚═════╝ ╚══════╝
#include <bits/stdc++.h>

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
using namespace std;

#define fst first
#define snd second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back

#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
#define sqr(x) ((x) * (x))

#define ADD_OPERATORS_IN(T, COMP) \
    bool operator < (const T& ot) const { return COMP(ot) == -1; } \
    bool operator > (const T& ot) const { return COMP(ot) == 1; } \
    bool operator == (const T& ot) const { return COMP(ot) == 0; } \
    bool operator != (const T& ot) const { return COMP(ot) != 0; }

#define ADD_OPERATORS_OUT(T, COMP) \
    bool operator < (const T& a, const T& b) const { return COMP(a, b) == -1; } \
    bool operator > (const T& a, const T& b) const { return COMP(a, b) == 1; } \
    bool operator == (const T& a, const T&b) const { return COMP(a, b) == 0; } \
    bool operator != (const T& a, const T&b) const { return COMP(a, b) != 0; }


typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;

mt19937_64 mt_rand(chrono::system_clock::now().time_since_epoch().count());

template<typename T1, typename T2> inline bool upmax(T1& a, T2 b) { return (a < b ? (a = b, true) : false); }
template<typename T1, typename T2> inline bool upmin(T1& a, T2 b) { return (b < a ? (a = b, true) : false); }

const int maxn = (int) 1e6+10;
const int maxlog = 21;
const int base = 998'244'353;
const ld eps = (ld) 1e-9;
const ld PI = acos(-1.);
const int pp = 41;
const int INF = (int) 2e9;
const ll llinf = (ll) 1e18;

int n, m;
int t[4 * maxn];

void bld(int v, int l, int r) {
  if (l == r) {
    t[v] = 1;
  } else {
    int m = (l + r) >> 1;
    bld(v + v, l, m);
    bld(v + v + 1, m + 1, r);
    t[v] = t[v + v] + t[v + v + 1];
  }
}

void upd(int v, int l, int r, int pos) {
  if (l == r) {
    t[v] = 0;
  } else {
    int m = (l + r) >> 1;
    if (pos <= m) upd(v + v, l, m, pos);
    else upd(v + v + 1, m + 1, r, pos);
    t[v] = t[v + v] + t[v + v + 1];
  }
}

int getsum(int v, int tl, int tr, int l, int r) {
  if (tl == l && tr == r) {
    return t[v];
  }
  int m = (tl + tr) >> 1;
  int ans = 0;
  if (l <= m) ans += getsum(v + v, tl, m, l, min(r, m));
  if (r > m) ans += getsum(v + v + 1, m + 1, tr, max(l, m + 1), r);
  return ans;
}

int getpos(int pos) {
  int l = 1;
  int r = pos;
  int all = getsum(1, 1, m, 1, pos);
  if (all == 0) {
    return -1;
  }
  while (l != r) {
    int mid = (l + r) >> 1;
    if (getsum(1, 1, m, 1, mid) < all) {
      l = mid + 1;
    } else {
      r = mid;
    }
  }
  return l;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

//  freopen("input.txt", "r", stdin);
  cin >> n >> m;
  vector<pii> a(n);
  for (int i = 0; i < n; i ++) {
    cin >> a[i].fst >> a[i].snd;
  }

  bld(1, 1, m);

  sort(all(a), [&](auto x, auto y) {
    if (x.snd == y.snd) {
      return x.fst < y.fst;
    }
    return x.snd > y.snd;
  });

  int ans = 0;
  for (auto & it : a) {
    if (it.first > m) continue;
//    cout << m - it.first + 1 << " " << it.second << " -> pos ";
    int ps = getpos(m - it.first + 1);
//    cout << ps << "\n";?
    if (ps < 0) continue;
    upd(1, 1, m, ps);
    ans += it.second;
  }
  cout << ans ;

  return 0;
}

Measured memory_bytes: 2048