#include <bits/stdc++.h>
using namespace std;
const long long ThaoNguyen = 1e5 + 5, mod = 1e9 + 7;
long long n, m, ans = 0, it = 0, it1[ThaoNguyen];
string t;
pair<long long, long long> a[100005];
long long sum1[ThaoNguyen];
vector<long long> s[ThaoNguyen], use[ThaoNguyen];
signed main() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i].first >> a[i].second;
  }
  sort(a + 1, a + n + 1);
  a[0].first = -1, a[0].second = -1, s[0].push_back(-1);
  a[n + 1].first = -1, a[n + 1].second = -1;
  for (long long i = 1; i <= n + 1; i++) {
    if (a[i].first != a[i - 1].first) {
      it++;
      sort(s[it - 1].begin(), s[it - 1].end());
      s[it].push_back(a[i].second);
    } else {
      s[it].push_back(a[i].second);
    }
  }
  it--;
  for (long long i = 1; i <= it; i++) {
    reverse(s[i].begin(), s[i].end());
    use[0].push_back(i);
  }
  for (long long i = 1; i <= 1000000000; i++) {
    if (use[i - 1].size() == 0) {
      cout << ans;
      return 0;
    }
    long long cursum = 0;
    for (long long j = 0; j < use[i - 1].size(); j++) {
      sum1[use[i - 1][j]] += s[use[i - 1][j]][i - 1];
      if (sum1[use[i - 1][j]] > 0) {
        cursum += sum1[use[i - 1][j]];
      }
      if (s[use[i - 1][j]].size() > i) {
        use[i].push_back(use[i - 1][j]);
      }
    }
    ans = max(ans, cursum);
  }
}
