#include <bits/stdc++.h>
using namespace std;
long long n, m, a, b;
vector<vector<long long> > h;
bool cmp(vector<long long> a, vector<long long> b) {
  return (int)(a).size() < (int)(b).size();
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  h.resize(m + 1);
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    h[a].push_back(b);
  }
  for (int i = 0; i < (int)(h).size(); i++)
    if ((int)(h[i]).size() > 0) sort(h[i].rbegin(), h[i].rend());
  vector<long long> sumv(n + 1, 0);
  vector<long long> sumh(n + 1, 0);
  for (int i = 1; i <= m; i++) {
    long long gs = 0;
    for (int j = 0; j < (int)(h[i]).size(); j++)
      if (gs + h[i][j] >= 0)
        gs += h[i][j];
      else
        h[i][j] = 0;
  }
  for (int i = 1; i <= m; i++)
    while ((int)(h[i]).size() > 0 && h[i].back() == 0) h[i].pop_back();
  for (int i = 1; i <= m; i++) {
    int k = (int)(h[i]).size();
    for (int j = 0; j < (int)(h[i]).size(); j++) sumh[k] += h[i][j];
  }
  for (int i = 1; i <= m; i++)
    for (int j = 0; j < (int)(h[i]).size(); j++) sumv[j] += h[i][j];
  long long ans = 0, zt = 0;
  for (int i = 0; i <= n; i++) {
    zt -= sumh[i];
    zt += sumv[i];
    ans = max(ans, zt);
  }
  cout << ans;
}
