#include <bits/stdc++.h>
using namespace std;
const int Z = (int)3e3 + 228;
const int N = (int)3e5 + 228;
const int INF = (int)1e9 + 228;
const int MOD = (int)1e9 + 7;
string s[N];
int cost[N];
bool is_pal(string t) {
  int n = t.size();
  for (int i = 0; i < n / 2; i++)
    if (t[i] != t[n - i - 1]) return false;
  return true;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int k, n;
  cin >> k >> n;
  map<string, vector<int> > mp;
  for (int i = 0; i < k; i++) {
    string s;
    int c;
    cin >> s >> c;
    mp[s].push_back(c);
  }
  for (auto it = mp.begin(); it != mp.end(); it++)
    sort((it->second).rbegin(), (it->second).rend());
  int mx = 0, ans = 0;
  for (auto it : mp)
    if (is_pal(it.first)) {
      vector<int> v = it.second;
      for (int i = 0; i < v.size() - 1; i += 2)
        if (v[i] + v[i + 1] >= 0) {
          ans += v[i] + v[i + 1];
          mx = max(mx, -v[i + 1]);
        } else
          mx = max(mx, v[i]);
      if (v.size() % 2) mx = max(mx, v.back());
    } else {
      vector<int> v = it.second;
      string t = it.first;
      reverse(t.begin(), t.end());
      if (mp.find(t) == mp.end()) continue;
      vector<int> rev = mp[t];
      for (int i = 0; i < min(v.size(), rev.size()); i++)
        if (v[i] + rev[i] > 0) ans += v[i] + rev[i];
      (it.second).clear();
      (mp[t]).clear();
    }
  cout << ans + mx;
  return 0;
}
