#include <bits/stdc++.h>
using namespace std;
map<string, vector<int>> a;
bool check(string s, string t) {
  for (int i = 0; i < s.size(); i++) {
    if (s[i] != t[i]) return false;
  }
  return true;
}
int main() {
  int k, n;
  cin >> k >> n;
  for (int i = 0; i < k; i++) {
    string s;
    cin >> s;
    int num;
    cin >> num;
    a[s].push_back(num);
  }
  for (auto it = a.begin(); it != a.end(); it++)
    sort(it->second.begin(), it->second.end());
  long long ans = 0;
  for (auto it = a.begin(); it != a.end(); it++) {
    string temp = it->first;
    reverse(temp.begin(), temp.end());
    if (check(temp, it->first)) continue;
    auto itt = a.find(temp);
    if (itt == a.end()) continue;
    while (it->second.size() > 0 && itt->second.size() > 0) {
      int tmp = it->second.back() + itt->second.back();
      if (tmp <= 0) break;
      ans += tmp;
      it->second.pop_back();
      itt->second.pop_back();
    }
  }
  int add = 0;
  for (auto it = a.begin(); it != a.end(); it++) {
    string temp = it->first;
    reverse(temp.begin(), temp.end());
    if (!check(temp, it->first)) continue;
    int i;
    for (i = (int)it->second.size() - 1; i > 0; i -= 2) {
      if (it->second[i - 1] < 0) break;
      int tmp = it->second[i] + it->second[i - 1];
      ans += tmp;
    }
    if (i == 0)
      add = max(add, it->second[i]);
    else if (i >= 1) {
      if (it->second[i] + it->second[i - 1] < 0)
        add = max(add, it->second[i]);
      else {
        ans += it->second[i] + it->second[i - 1];
        add = max(add, -it->second[i - 1]);
      }
    }
  }
  ans += add;
  cout << ans << endl;
}
