#include <bits/stdc++.h>
using namespace std;
map<string, priority_queue<long long> > choi;
set<string> used;
long long psum[200005];
long long psum2[200005];
string reverse(string A) {
  string B = "";
  for (long long a = (long long)A.length() - 1; a >= 0; a--) B += A[a];
  return B;
}
int main() {
  long long a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q;
  string A, B, C, D, E;
  char X[100005];
  scanf("%lld %lld", &a, &b);
  for (c = 1; c <= a; c++) {
    scanf(" %s%lld", &X, &d);
    choi[string(X)].push(d);
  }
  g = 0;
  h = 0;
  for (auto it : choi) {
    A = it.first;
    B = reverse(A);
    if (used.count(A) || used.count(B)) continue;
    used.insert(A);
    used.insert(B);
    if (A == B) {
      auto x = it.second;
      while (!x.empty()) {
        e = x.top();
        x.pop();
        if (e <= 0) break;
        if (x.empty()) {
          psum[++h] = e;
          psum2[h] = 0;
          break;
        }
        f = x.top();
        x.pop();
        if (f < 0 && e + f > 0) {
          psum[++h] = e;
          psum2[h] = e + f;
          break;
        }
        if (e + f <= 0) {
          psum[++h] = e;
          psum2[h] = 0;
          break;
        }
        g += e + f;
      }
    } else {
      auto x = it.second;
      auto y = choi[B];
      while (!x.empty() && !y.empty()) {
        e = x.top();
        f = y.top();
        x.pop();
        y.pop();
        if (e + f <= 0) break;
        g += e + f;
      }
    }
  }
  psum[0] = psum2[0] = 0;
  for (c = 1; c <= h; c++) {
    psum[c] += psum[c - 1];
    psum2[c] += psum2[c - 1];
  }
  j = 0;
  for (c = 1; c <= h; c++) {
    i = psum[c] - psum[c - 1] + psum2[h] - (psum2[c] - psum2[c - 1]);
    j = max(j, i);
  }
  g += j;
  printf("%lld\n", g);
  return 0;
}
