#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long s = 0, f = 1;
  char ch;
  while (!isdigit(ch = getchar())) (ch == '-') && (f = -1);
  for (s = ch - '0'; isdigit(ch = getchar()); s = s * 10 + ch - '0')
    ;
  return s * f;
}
void putit(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) putit(x / 10);
  putchar('0' + x % 10);
}
bool cmp(long long aa, long long bb) { return aa > bb; }
long long n, m;
vector<long long> vec[110007];
long long len[110007], maxn, ans, cha[110007];
int main() {
  n = read(), m = read();
  for (int i = 1, xi, yi; i <= n; i++)
    xi = read(), yi = read(), vec[xi].push_back(yi), len[xi]++,
    maxn = max(maxn, len[xi]);
  for (int i = 1; i <= m; i++) sort(vec[i].begin(), vec[i].end(), cmp);
  for (int i = 1; i <= m; i++) {
    if (len[i] == 0) continue;
    if (vec[i][0] > 0) ans += vec[i][0];
    if (len[i] == 1) continue;
    for (int j = 1; j < len[i]; j++) {
      vec[i][j] += vec[i][j - 1];
      if (vec[i][j] > 0) cha[j + 1] += vec[i][j];
    }
  }
  for (int i = 2; i <= maxn; i++) ans = max(ans, cha[i]);
  putit(ans), puts("");
  return 0;
}
