#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m, dp[maxn][2], num, cnt, p[maxn * 2];
vector<int> a[maxn], b[maxn * 2];
map<string, int> ma, mp;
int main() {
  scanf("%d%d", &n, &m);
  string s, t;
  num = cnt = 0;
  for (int i = 1; i <= n; i++) {
    cin >> s;
    t = s;
    reverse(t.begin(), t.end());
    int val;
    scanf("%d", &val);
    if (s == t) {
      if (!ma[s]) {
        ma[s] = ++num;
        a[num].push_back(-val);
      } else {
        int k = ma[s];
        a[k].push_back(-val);
      }
    } else {
      if (!mp[s]) {
        mp[s] = ++cnt;
        b[cnt].push_back(-val);
      } else {
        int k = mp[s];
        b[k].push_back(-val);
      }
      if (!mp[t]) mp[t] = ++cnt;
      p[mp[s]] = mp[t];
      p[mp[t]] = mp[s];
    }
  }
  for (int i = 1; i <= num; i++) sort(a[i].begin(), a[i].end());
  int ans = 0;
  for (int i = 1; i <= num; i++) {
    dp[i][1] = dp[i - 1][1];
    dp[i][0] = dp[i - 1][0];
    int sum = 0;
    for (int j = 1; j <= a[i].size(); j++) {
      sum -= a[i][j - 1];
      if (j & 1)
        dp[i][1] = max(dp[i][1], dp[i - 1][0] + sum);
      else {
        dp[i][0] = max(dp[i][0], dp[i - 1][0] + sum);
        dp[i][1] = max(dp[i][1], dp[i - 1][1] + sum);
      }
    }
  }
  ans = max(dp[num][1], dp[num][0]);
  int tmp = 0;
  for (int i = 1; i <= cnt; i++) sort(b[i].begin(), b[i].end());
  for (int i = 1; i <= cnt; i++) {
    int sum = 0, tt = 0;
    for (int j = 1; j <= min(b[i].size(), b[p[i]].size()); j++) {
      sum -= b[i][j - 1] + b[p[i]][j - 1];
      if (sum > tt)
        tt = sum;
      else
        break;
    }
    tmp += tt;
  }
  ans += tmp / 2;
  printf("%d\n", ans);
  return 0;
}
