#include <bits/stdc++.h>
using namespace std;
const long long mx = 105;
const long long maxn = 1000001;
const long long mod = 1000000007;
vector<vector<int>> adj;
vector<int> ord;
vector<pair<long, long>> f1;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long t = 1;
  while (t--) {
    long long n, m, i, j, k, a, b;
    cin >> n >> m;
    vector<vector<long long>> spv(m + 1);
    for (i = 0; i < n; i++) {
      cin >> a >> b;
      spv[a].push_back(b);
    }
    for (i = 1; i <= m; i++) {
      sort(spv[i].rbegin(), spv[i].rend());
    }
    long long mx[n + 1];
    memset(mx, 0, sizeof(mx));
    for (i = 1; i <= m; i++) {
      for (j = 0; j < (int)spv[i].size(); j++) {
        if (j > 0) spv[i][j] = spv[i][j - 1] + spv[i][j];
        if (spv[i][j] < 0) {
          break;
        }
        mx[j] += spv[i][j];
      }
    }
    cout << *max_element(mx, mx + n + 1);
  }
}
