#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double EPS = 1e-6;
const int infi = (int)1e9;
const long long infl = (long long)1e18;
const int MOD = (int)1e9 + 7;
const int MAX = (int)1e6 + 9;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, -1, 1, 1, -1, 1, -1};
long long n, k, val, res, ans;
map<string, priority_queue<long long> > mp;
map<string, long long> cnt;
pair<string, long long> a[MAX];
string s, t;
bool pal(string s) {
  for (int i = 0, j = n - 1; i < j; i++, j--) {
    if (s[i] != s[j]) return 0;
  }
  return 1;
}
int main() {
  cin >> k >> n;
  for (int i = 0; i < k; i++) {
    cin >> s >> val;
    mp[s].push(val);
    cnt[s]++;
    a[i].first = s;
    a[i].second = val;
  }
  long long mn = 0, mx = 0;
  for (int i = 0; i < k; i++) {
    s = a[i].first;
    t = s;
    reverse(t.begin(), t.end());
    if (cnt[s] <= 0 || cnt[t] <= 0) continue;
    if (pal(s) && cnt[s] < 2) continue;
    val = mp[s].top();
    mp[s].pop();
    res = mp[t].top();
    mp[t].pop();
    if (res + val <= 0) {
      mp[s].push(val);
      mp[t].push(res);
      continue;
    }
    if (pal(s)) {
      if (res < mn) {
        mn = res;
        mx = val;
      }
    }
    ans += (res + val);
    cnt[s]--;
    cnt[t]--;
  }
  res = 0;
  for (int i = 0; i < k; i++) {
    s = a[i].first;
    if (cnt[s] <= 0) continue;
    if (pal(s)) res = max(res, mp[s].top());
  }
  long long a1 = ans + res;
  long long a2 = ans - mn;
  cout << max(a1, a2) << "\n";
  return 0;
}
