#include <bits/stdc++.h>
using namespace std;
long long i, n, k, val, ans, ans1, val2, val1, value, x;
string s, temp;
map<string, multiset<long long> > nor, pal;
map<string, multiset<long long> >::iterator it;
set<long long>::iterator it1, it2;
int main() {
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> s >> val;
    temp = s;
    reverse(temp.begin(), temp.end());
    if (temp == s)
      pal[s].insert(val);
    else
      nor[s].insert(val);
  }
  for (it = nor.begin(); it != nor.end(); it++) {
    s = it->first;
    temp = s;
    reverse(temp.begin(), temp.end());
    while (nor[temp].size() != 0 && nor[s].size() != 0) {
      it1 = nor[temp].end();
      it1--;
      val1 = *it1;
      it2 = nor[s].end();
      it2--;
      val2 = *it2;
      nor[temp].erase(--nor[temp].end());
      nor[s].erase(--nor[s].end());
      if (val1 + val2 > 0)
        ans += val1 + val2;
      else
        break;
    }
    nor[temp].clear();
    nor[s].clear();
  }
  for (it = pal.begin(); it != pal.end(); it++) {
    s = it->first;
    for (; pal[s].size() > 1; ans += max(val, x)) {
      it1 = pal[s].end();
      it1--;
      val1 = *it1;
      pal[s].erase(--pal[s].end());
      it2 = pal[s].end();
      it2--;
      val2 = *it2;
      pal[s].erase(--pal[s].end());
      val = val1 + val2;
      if (val1 > 0 && val2 < 0) ans1 = max(ans1, val1 - max(val, x));
    }
    if (pal[s].size()) {
      it1 = pal[s].end();
      it1--;
      value = *it1;
      ans1 = max(ans1, value);
      pal[s].clear();
    }
    pal[s].clear();
  }
  cout << ans + ans1 << endl;
}
