#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
const long long inf = 0x3f3f3f3f3f3f3f3fll;
using namespace std;
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
void d1(long long x) { cout << x << endl; }
void d2(long long x, long long y) { cout << x << " " << y << endl; }
void d3(long long x, long long y, long long z) {
  cout << x << " " << y << " " << z << endl;
}
void d4(long long w, long long x, long long y, long long z) {
  cout << w << " " << x << " " << y << " " << z << endl;
}
void da(long long a[], long long n) {
  for (long long i = 0; i < n; i++) cout << a[i] << " ";
  cout << endl;
}
void dv(vector<long long>& v) {
  for (long long i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << endl;
}
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  long long n, m, i, x, y, j, ans = -inf;
  cin >> n >> m;
  vector<vector<long long> > v(m + 1);
  long long a[100001];
  for (i = 0; i < 100001; i++) a[i] = 0;
  for (i = 0; i < n; i++) {
    cin >> x >> y;
    v[x].push_back(y);
  }
  for (i = 1; i < m + 1; i++)
    sort(v[i].begin(), v[i].end(), greater<long long>()), a[i] = 0;
  for (i = 1; i < m + 1; i++) {
    for (j = 1; j < v[i].size(); j++) {
      v[i][j] += v[i][j - 1];
    }
  }
  for (i = 1; i < m + 1; i++) {
    for (j = 0; j < v[i].size(); j++) {
      if (v[i][j] > 0) {
        a[j + 1] += v[i][j];
      } else
        break;
    }
  }
  for (i = 1; i < 100001; i++) ans = ((ans) > (a[i]) ? (ans) : (a[i]));
  if (ans < 0)
    cout << -1;
  else
    cout << ans;
  return 0;
}
