#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
const long long INF = 1e18;
const int B = 1e9 + 9;
map<string, int> mp;
int sz;
int ip[N];
vector<int> l[N], r[N];
int g(string &s) {
  if (mp.count(s) == 0) return mp[s] = ++sz;
  return mp[s];
}
int main() {
  ios_base::sync_with_stdio(0);
  int n, k;
  cin >> n >> k;
  for (int i = int(1); i <= int(n); ++i) {
    string s;
    int cost;
    cin >> s >> cost;
    string t = s;
    reverse(t.begin(), t.end());
    if (s == t) {
      int id = g(s);
      ip[id] = 1;
      l[id].push_back(cost);
    }
    if (s < t) l[g(s)].push_back(cost);
    if (s > t) r[g(t)].push_back(cost);
  }
  int ans = 0, mid = 0;
  for (int i = int(1); i <= int(sz); ++i) {
    sort(l[i].begin(), l[i].end(), greater<int>());
    sort(r[i].begin(), r[i].end(), greater<int>());
    if (ip[i] == 1) {
      int x = 0;
      for (; x + 1 < l[i].size() && l[i][x] + l[i][x + 1] > 0; x += 2)
        ans += l[i][x] + l[i][x + 1];
      if (x - 1 < l[i].size() && x - 1 >= 0) mid = max(mid, -l[i][x - 1]);
      if (x < l[i].size()) mid = max(mid, l[i][x]);
    } else {
      int x = 0;
      for (; x < min(l[i].size(), r[i].size()) && l[i][x] + r[i][x] > 0; ++x)
        ans += l[i][x] + r[i][x];
    }
  }
  cout << ans + mid << "\n";
  return 0;
}
