#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
bool prime[1000001];
vector<int> v1;
void sieve() {
  memset(prime, true, sizeof(prime));
  for (int i = 2; i * i <= 1000000; i++)
    if (prime[i])
      for (int j = i * i; j <= 1000000; j += i) prime[j] = false;
  prime[0] = prime[1] = false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<int> v1[m + 1];
  for (int i = 0; i < n; i++) {
    int a1, a2;
    cin >> a1 >> a2;
    v1[a1].push_back(a2);
  }
  for (int i = 1; i < m + 1; i++) {
    sort((v1[i]).begin(), (v1[i]).end());
    reverse((v1[i]).begin(), (v1[i]).end());
  }
  int mx = 0;
  for (int i = 1; i < m + 1; i++) {
    mx = max(mx, (int)v1[i].size());
    for (int j = 1; j < v1[i].size(); j++) {
      v1[i][j] = v1[i][j - 1] + v1[i][j];
    }
  }
  vector<int> res(mx, 0);
  int max1 = 0;
  for (int i = 1; i < m + 1; i++) {
    for (int j = 0; j < v1[i].size(); j++) {
      if (v1[i][j] < 0) break;
      res[j] += v1[i][j];
    }
  }
  int ans = 0;
  cout << max(ans, *max_element(res.begin(), res.end())) << endl;
  return 0;
}
