#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int INF = 0x3f3f3f3f;
void read(int &val) {
  int x = 0;
  int bz = 0;
  char c;
  for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
    ;
  if (c == '-') {
    bz = 1;
    c = getchar();
  }
  for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48;
  if (bz) x = -x;
  val = x;
}
vector<int> que[maxn];
int ans[maxn], n, m;
long long sum[maxn], ss[maxn];
int main() {
  read(n);
  read(m);
  memset(sum, 0, sizeof(sum));
  memset(ss, 0, sizeof(ss));
  int a, b;
  for (int i = 1; i <= n; i++) {
    read(a);
    read(b);
    que[a].push_back(b);
  }
  int maxsz = 0;
  for (int i = 1; i <= m; i++) {
    sort(que[i].begin(), que[i].end());
    reverse(que[i].begin(), que[i].end());
    int sz = que[i].size();
    maxsz = max(sz, maxsz);
    for (int j = 0; j <= sz - 1; j++) {
      sum[i] += que[i][j];
      if (sum[i] >= 0) {
        ss[j] += sum[i];
      } else
        break;
    }
  }
  long long ans = -1;
  for (int i = 0; i <= maxsz - 1; i++) {
    ans = max(ss[i], ans);
  }
  printf("%lld\n", ans);
  return 0;
}
