#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1e9 + 7;
const long long LINF = (long long)2e18 + 7;
int K, N;
vector<pair<string, int>> V;
map<pair<string, string>, pair<vector<int>, vector<int>>> Mp;
map<string, vector<int>> One;
int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    string st;
    int a;
    cin >> st;
    cin >> a;
    string n1 = st;
    reverse(n1.begin(), n1.end());
    if (st == n1) {
      One[st].push_back(a);
      continue;
    }
    if (st < n1) {
      Mp[make_pair(st, n1)].first.push_back(a);
    } else {
      Mp[make_pair(n1, st)].second.push_back(a);
    }
    V.push_back(make_pair(st, a));
  }
  long long ans = 0;
  int mn = 0, mx = 0;
  for (auto el : One) {
    sort(el.second.begin(), el.second.end());
    reverse(el.second.begin(), el.second.end());
    for (int i = 0; i < el.second.size(); i += 2) {
      if (i + 1 != el.second.size() && el.second[i + 1] + el.second[i] >= 0) {
        ans += el.second[i];
        ans += el.second[i + 1];
        if (el.second[i] < 0)
          mn = min(mn, el.second[i]);
        else if (el.second[i + 1] < 0)
          mn = min(mn, el.second[i + 1]);
      } else {
        mx = max(mx, el.second[i]);
        if (i + 1 < el.second.size()) mx = max(mx, el.second[i + 1]);
      }
    }
  }
  ans = max(ans, max(ans + mx, ans - mn));
  set<pair<string, string>> used;
  for (auto el : Mp) {
    if (used.count(el.first) ||
        used.count(make_pair(el.first.second, el.first.first)))
      continue;
    used.insert(el.first);
    sort(el.second.first.begin(), el.second.first.end());
    reverse(el.second.first.begin(), el.second.first.end());
    sort(el.second.second.begin(), el.second.second.end());
    reverse(el.second.second.begin(), el.second.second.end());
    for (int i = 0; i < min(el.second.first.size(), el.second.second.size());
         i++) {
      ans += max(0, el.second.first[i] + el.second.second[i]);
    }
  }
  cout << ans << "\n";
  return 0;
}
