#include <bits/stdc++.h>
using namespace std;
int n, m;
map<int, priority_queue<int>> map1;
map<int, priority_queue<int>> map2;
int main() {
  map1.clear();
  map2.clear();
  scanf("%d%d", &n, &m);
  int tmp1, tmp2;
  for (int i = 0; i < n; ++i) {
    scanf("%d%d", &tmp1, &tmp2);
    map1[tmp1].push(tmp2);
  }
  for (auto it = map1.begin(); it != map1.end(); ++it) {
    tmp1 = 0;
    tmp2 = 0;
    while (!((*it).second).empty()) {
      ++tmp1;
      tmp2 += ((*it).second).top();
      map2[tmp1].push(tmp2);
      (*it).second.pop();
    }
  }
  int ans = 0;
  for (auto it = map2.begin(); it != map2.end(); ++it) {
    tmp1 = 0;
    while (!((*it).second).empty()) {
      if ((*it).second.top() < 0) break;
      tmp1 += (*it).second.top();
      (*it).second.pop();
    }
    ans = max(ans, tmp1);
  }
  printf("%d\n", ans);
  return 0;
}
