#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 1e6 + 100;
map<string, vector<int>> mp;
bool is_pam(string s) {
  string t = s;
  reverse(t.begin(), t.end());
  return s == t;
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    string s;
    int num;
    cin >> s >> num;
    mp[s].push_back(num);
  }
  for (auto &it : mp) sort(it.second.begin(), it.second.end());
  long long ans = 0;
  int pam_mmin = inf;
  for (auto &it : mp) {
    if (is_pam(it.first)) {
      auto &node = it.second;
      while (node.size() >= 2 &&
             node[node.size() - 1] + node[node.size() - 2] > 0) {
        pam_mmin = min(pam_mmin, node[node.size() - 1]);
        pam_mmin = min(pam_mmin, node[node.size() - 2]);
        ans += node[node.size() - 1] + node[node.size() - 2];
        node.pop_back();
        node.pop_back();
      }
    } else {
      string s = it.first;
      reverse(s.begin(), s.end());
      auto &node1 = it.second, &node2 = mp[s];
      while (node1.size() && node2.size() && node1.back() + node2.back() > 0) {
        ans += node1.back() + node2.back();
        node1.pop_back();
        node2.pop_back();
      }
    }
  }
  long long mmax = 0;
  if (pam_mmin < 0) mmax = abs(pam_mmin);
  for (auto it : mp)
    if (is_pam(it.first) && it.second.size())
      mmax = max(mmax, (long long)it.second.back());
  printf("%lld\n", ans + mmax);
  return 0;
}
