#include <bits/stdc++.h>
using namespace std;
vector<int> a[100010];
int cmp(const int a, const int b) { return a > b; }
int sum[100010];
int main() {
  int n, m;
  while (cin >> n >> m) {
    vector<int>::iterator it;
    int x, y;
    memset(sum, 0, sizeof(sum));
    for (int i = 0; i < n; i++) {
      cin >> x >> y;
      a[x].push_back(y);
    }
    for (int i = 1; i <= n; i++) {
      sort(a[i].begin(), a[i].end(), cmp);
    }
    for (int i = 1; i <= m; i++) {
      for (int j = 1; j < a[i].size(); j++) {
        a[i][j] += a[i][j - 1];
      }
    }
    int ans = 0;
    for (int i = 1; i <= m; i++) {
      for (int j = 0; j < a[i].size(); j++) {
        if (a[i][j] > 0) {
          sum[j] += a[i][j];
          ans = max(ans, sum[j]);
        }
      }
      a[i].clear();
    }
    printf("%d\n", ans);
  }
  return 0;
}
