#include <bits/stdc++.h>
using namespace std;
struct node {
  long skl;
  node() {}
  node(long _skl) { skl = _skl; }
  bool operator<(const node &b) const { return skl > b.skl; }
};
vector<node> vec[100009];
vector<long> s;
int main() {
  long i, j, k, l, m, n, p, q, sm;
  cin >> n >> m;
  while (n--) {
    cin >> p >> q;
    vec[p].push_back(node(q));
  }
  for (i = 1; i <= m; i++) {
    sort(vec[i].begin(), vec[i].end());
  }
  s.resize(100009);
  for (i = 1; i <= m; i++) {
    sm = 0;
    for (j = 0; j < vec[i].size(); j++) {
      sm += vec[i][j].skl;
      if (sm > 0) s[j + 1] += sm;
    }
  }
  sort(s.begin(), s.end());
  cout << s[s.size() - 1] << endl;
  return 0;
}
