#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  multiset<long long> mst[m + 1];
  for (int i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    mst[a].insert(b);
  }
  multiset<long long>::iterator itr;
  set<long long> store;
  for (int i = 1; i <= m; i++) {
    if (mst[i].size() >= 1) store.insert(i);
  }
  long long ans = 0;
  set<long long>::iterator itr1, itr2;
  vector<long long> bhos;
  while (!store.empty()) {
    long long temp = 0;
    bhos.clear();
    for (itr1 = store.begin(); itr1 != store.end(); ++itr1) {
      itr = mst[*itr1].end();
      itr--;
      if (*itr < 0) {
        bhos.push_back(*itr1);
        continue;
      } else {
        temp += *itr;
        long long just = *itr;
        mst[*itr1].erase(itr);
        if (mst[*itr1].size() >= 1) {
          itr = mst[*itr1].end();
          itr--;
          just += *itr;
          if (just < 0) bhos.push_back(*itr1);
          mst[*itr1].erase(itr);
          mst[*itr1].insert(just);
        } else if (mst[*itr1].size() == 0)
          bhos.push_back(*itr1);
      }
    }
    for (int i = 0; i < bhos.size(); i++) {
      store.erase(bhos[i]);
    }
    ans = max(ans, temp);
  }
  cout << ans << endl;
}
