#include <bits/stdc++.h>
using namespace std;
template <typename T>
T id(T b) {
  return b;
};
template <class It>
bool all(It f, It l) {
  return std::all_of(f, l, id<bool>);
}
template <class It>
bool any(It f, It l) {
  return std::any_of(f, l, id<bool>);
}
template <typename T>
void chmax(T &x, T y) {
  if (x < y) x = y;
}
template <typename T>
void chmin(T &x, T y) {
  if (x > y) x = y;
}
int n, m;
map<int, vector<int> > cnt;
vector<int> cur;
map<int, int> total;
int main() {
  cin >> n >> m;
  for (int i = 0; i < int(n); i++) {
    int s, r;
    cin >> s >> r;
    cur.push_back(s);
    cnt[s].push_back(r);
  }
  for (auto &p : cnt) sort(p.second.begin(), p.second.end());
  sort(cur.begin(), cur.end());
  cur.erase(unique(cur.begin(), cur.end()), cur.end());
  long long s = 0;
  long long res = 0;
  while (!cnt.empty()) {
    long long rm = 0;
    vector<int> temp;
    for (int p : cur) {
      total[p] += cnt[p].back();
      s += cnt[p].back();
      cnt[p].pop_back();
      if (total[p] < 0) {
        s -= total[p];
        cnt.erase(p);
      } else if (cnt[p].size() == 0) {
        rm += total[p];
        cnt.erase(p);
      } else {
        temp.push_back(p);
      }
    }
    chmax(res, s);
    s -= rm;
    cur = temp;
  }
  cout << res << endl;
  return 0;
}
