#include <bits/stdc++.h>
using namespace std;
map<string, vector<int>> mp;
set<string> st;
char str[200005];
string s;
int main() {
  int k, n, i, ans = 0, val, mx = 0;
  cin >> k >> n;
  for (i = 0; i < k; i++) {
    scanf("%s%d", str, &val);
    s = string(str);
    st.insert(s);
    mp[s].push_back(val);
  }
  for (auto z : st) {
    s = z;
    reverse(s.begin(), s.end());
    if (s < z) continue;
    if (s == z) {
      sort(mp[s].begin(), mp[s].end(), greater<int>());
      for (i = 0; i + 1 < mp[s].size(); i += 2)
        if (mp[s][i] + mp[s][i + 1] >= 0) {
          ans += mp[s][i] + mp[s][i + 1];
          mx = max(mx, -mp[s][i + 1]);
        } else
          break;
      if (i < mp[s].size()) mx = max(mx, mp[s][i]);
      continue;
    }
    sort(mp[s].begin(), mp[s].end(), greater<int>());
    sort(mp[z].begin(), mp[z].end(), greater<int>());
    for (i = 0; i < mp[s].size() && i < mp[z].size(); i++)
      if (mp[s][i] + mp[z][i] >= 0) ans += mp[s][i] + mp[z][i];
  }
  printf("%d\n", ans + mx);
}
