#include <bits/stdc++.h>
using namespace std;
const int64_t N = 5e6;
const int64_t mod = 1e9 + 7;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int64_t n, m;
  cin >> n >> m;
  vector<vector<int64_t>> a(m);
  for (int64_t i = 1; i <= n; i++) {
    int64_t s, r;
    cin >> s >> r;
    a[--s].push_back(r);
  }
  vector<int64_t> f(n);
  for (auto it : a) {
    if (!it.size()) continue;
    sort(it.rbegin(), it.rend());
    int64_t cur = 0;
    for (int64_t i = 0; i < it.size(); i++) {
      cur += it[i];
      if (cur > 0)
        f[i] += cur;
      else
        break;
    }
  }
  cout << *max_element(f.begin(), f.end());
}
