#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long long int INF = 9e18 + 2e17;
const int inf = 2e9;
const int N = 1e5 + 22;
const double eps = 1e-10;
bool cmp(pair<int, int> A, pair<int, int> B) {
  if (A.first != B.first) return A.first < B.first;
  return A.second > B.second;
}
void _solve() {
  int n, m;
  cin >> n >> m;
  pair<int, int> sr[n];
  for (int i = (int)0; i < (int)n; i++) cin >> sr[i].first >> sr[i].second;
  sort(sr, sr + n, cmp);
  int c = 0;
  for (int i = 0; i < n;) {
    int x = sr[i].first;
    while (i < n && x == sr[i].first) i++;
    c++;
  }
  vector<vector<int> > row_sum(c);
  vector<int> tot_sum;
  int p = 0;
  for (int i = 0; i < n;) {
    int x = sr[i].first, sum = 0;
    while (i < n && x == sr[i].first) {
      sum += sr[i].second;
      i++;
      if (sum <= 0) continue;
      row_sum[p].push_back(sum);
    }
    if (!row_sum[p].empty()) p++;
  }
  pair<int, int> row_sz[p];
  for (int i = (int)0; i < (int)p; i++) row_sz[i] = {row_sum[i].size(), i};
  sort(row_sz, row_sz + p);
  for (int y = 0, i = 0; y < p;) {
    int x = row_sz[y].first;
    while (i < x) {
      int sum = 0;
      for (int j = (int)y; j < (int)p; j++) sum += row_sum[row_sz[j].second][i];
      tot_sum.push_back(sum);
      i++;
    }
    while (x == row_sz[y].first) y++;
  }
  int ans = 0;
  for (int i = (int)0; i < (int)tot_sum.size(); i++) ans = max(ans, tot_sum[i]);
  cout << ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  _solve();
  return 0;
}
