#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
using namespace std;
inline int read() {
  int r = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = r * 10 + c - '0';
    c = getchar();
  }
  return r;
}
inline long long readll() {
  long long r = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = r * 10 + c - '0';
    c = getchar();
  }
  return r;
}
inline long long qpow(long long a, long long b, long long mod) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
inline long long gcd(long long a, long long b) {
  while (b ^= a ^= b ^= a %= b)
    ;
  return a;
}
const double eps = 1e-8;
const long long LLINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int MAXN = 2e5 + 7;
const int MAXM = 2e5;
vector<int> f[MAXN];
int res[MAXN];
bool cmp(int a, int b) { return a > b; }
int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  int a, b;
  for (int i = 1; i <= n; ++i) {
    cin >> a >> b;
    f[a].push_back(b);
  }
  for (int i = 1; i <= m; ++i) {
    sort(f[i].begin(), f[i].end(), cmp);
    for (int j = 1; j < (int)f[i].size(); ++j) {
      f[i][j] += f[i][j - 1];
    }
    for (int j = 0; j < (int)f[i].size(); ++j) {
      res[j + 1] += max(0, f[i][j]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) ans = max(ans, res[i]);
  cout << ans << endl;
  return 0;
}
