#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
map<string, int> id;
bool isP[100005];
vector<int> Bs[100005];
int pr[100005], i[100005];
string getstr(int L) {
  string ans = "";
  getchar();
  for (int k = 0; k < (L); k++) ans.push_back(getchar());
  return ans;
}
bool larger(int a, int b) { return a > b; }
string rev(string s) {
  string ans;
  for (int k = s.length() - 1; k >= 0; k--) ans.push_back(s[k]);
  return ans;
}
bool pal(string s) { return s == rev(s); }
int main() {
  int n, l, IS = 0, cur, beau;
  scanf("%d%d", &n, &l);
  for (int k = 0; k < (n); k++) {
    string str = getstr(l);
    scanf("%d", &beau);
    if (id.find(str) != id.end())
      cur = id[str];
    else {
      if (pal(str)) isP[IS] = true;
      cur = id[str] = (IS++);
    }
    Bs[cur].push_back(beau);
  }
  for (map<string, int>::iterator it = id.begin(); it != id.end(); it++)
    if (id.find(rev(it->first)) != id.end())
      pr[it->second] = id[rev(it->first)];
    else
      pr[it->second] = -1;
  for (int k = 0; k < (IS); k++) sort(Bs[k].begin(), Bs[k].end(), larger);
  int bp = -1, sc = 0;
  for (int k = 0; k < (IS); k++) {
    if (isP[k]) {
      for (i[k] = 0; i[k] < Bs[k].size(); i[k] += 2) {
        if (Bs[k][i[k]] <= 0) break;
        if (i[k] == Bs[k].size() - 1) {
          bp = max(bp, Bs[k][i[k]]);
          i[k]++;
          break;
        }
        if (Bs[k][i[k]] + Bs[k][i[k] + 1] <= 0) {
          bp = max(bp, Bs[k][i[k]]);
          break;
        }
        sc += Bs[k][i[k]] + Bs[k][i[k] + 1];
        if (Bs[k][i[k] + 1] < 0) {
          bp = max(bp, -Bs[k][i[k] + 1]);
          break;
        }
      }
    } else {
      if (pr[k] == -1) continue;
      for (int j = 0; j < (min(Bs[k].size(), Bs[pr[k]].size())); j++) {
        if (Bs[k][j] + Bs[pr[k]][j] >= 0)
          sc += Bs[k][j] + Bs[pr[k]][j];
        else
          break;
      }
      pr[pr[k]] = -1;
      pr[k] = -1;
    }
  }
  if (bp != -1) sc += bp;
  printf("%d", sc);
  return 0;
}
