#include <bits/stdc++.h>
using namespace std;
map<string, vector<int> > palin;
map<string, vector<int> > regular;
int main() {
  string s, re;
  int n, k, Ti = 0, point, temp, mid = 0, mn = 0;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> s >> point;
    re = s;
    reverse(re.begin(), re.end());
    if (re == s)
      palin[s].push_back(point);
    else
      regular[s].push_back(point);
  }
  map<string, vector<int> >::iterator itr, itr2;
  for (itr = palin.begin(); itr != palin.end(); itr++) {
    sort((itr->second).begin(), (itr->second).end());
  }
  for (itr = regular.begin(); itr != regular.end(); itr++) {
    sort((itr->second).begin(), (itr->second).end());
  }
  for (itr = regular.begin(); itr != regular.end(); itr++) {
    s = re = itr->first;
    reverse(re.begin(), re.end());
    if (regular.count(re) != 0) {
      while (regular[s][regular[s].size() - 1] +
                     regular[re][regular[re].size() - 1] >
                 0 &&
             !regular[s].empty() && !regular[re].empty()) {
        temp = regular[s][regular[s].size() - 1] +
               regular[re][regular[re].size() - 1];
        Ti += temp;
        regular[s].pop_back();
        regular[re].pop_back();
      }
      regular.erase(re);
    }
  }
  for (itr = palin.begin(); itr != palin.end(); itr++) {
    s = itr->first;
    for (int i = palin[s].size() - 1; i > 0; i -= 2) {
      if (palin[s][i] >= 0 && palin[s][i - 1] >= 0)
        Ti += palin[s][i] + palin[s][i - 1];
      else if (palin[s][i] >= 0 && palin[s][i - 1] < 0) {
        temp = palin[s][i] + palin[s][i - 1];
        if (temp >= 0) {
          Ti += temp;
          if (palin[s][i] - temp > mid) mid = palin[s][i] - temp;
        } else
          mn = max(mn, palin[s][i]);
      } else
        break;
    }
    if (palin[s].size() % 2 && palin[s][0] >= 0) mn = max(palin[s][0], mn);
  }
  if (mn > mid)
    Ti += mn;
  else
    Ti += mid;
  cout << Ti << endl;
}
