#include <bits/stdc++.h>
using namespace std;
long long n, m, s, r, k, c, mx, a[200000], l;
pair<long long, long long> p[200000];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s >> r;
    p[i] = {s, -r};
  }
  sort(p, p + n);
  for (int i = 0; i < n; i++) {
    if (p[i].first != l) {
      k = 0;
      c = 0;
      l = p[i].first;
    }
    c -= p[i].second;
    k++;
    if (c > 0) a[k] += c;
    mx = max(mx, a[k]);
  }
  cout << mx;
  return 0;
}
