#include <bits/stdc++.h>
using namespace std;
int num[100005] = {0};
int temp[100005] = {0};
priority_queue<int> k[100005];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    k[x].push(y);
  }
  int mx = 0;
  for (int i = 1; i <= m; ++i) {
    mx = max(static_cast<int>(k[i].size()), mx);
  }
  for (int i = 1; i <= m; ++i) {
    int t = 1;
    while (!k[i].empty()) {
      temp[t] = temp[t - 1] + k[i].top();
      k[i].pop();
      if (temp[t] <= 0) {
        temp[t] = 0;
        break;
      }
      ++t;
    }
    temp[t] = 0;
    for (int i = 1; i <= mx; ++i) {
      if (temp[i] == 0) {
        break;
      }
      num[i] += temp[i];
    }
  }
  int re = 0;
  for (int i = 1; i <= mx; ++i) {
    re = max(num[i], re);
  }
  printf("%d\n", re);
  return 0;
}
