#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int M = 555;
pair<int, string> in[N];
unordered_map<string, vector<int> > Mp;
set<string> done;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long extra = 0;
  vector<pair<int, int> > dub;
  string ms;
  long long ans = 0;
  int mm, n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> in[i].second >> in[i].first;
  sort(in, in + n);
  for (int i = 0; i < n; i++) {
    Mp[in[i].second].push_back(in[i].first);
  }
  for (auto it : Mp) sort(Mp[it.first].begin(), Mp[it.first].end());
  for (auto it : Mp) reverse(Mp[it.first].begin(), Mp[it.first].end());
  for (auto it : Mp) {
    if (done.find(it.first) != done.end()) continue;
    string fr = it.first;
    vector<int> bt = it.second;
    string rv = fr;
    reverse(rv.begin(), rv.end());
    if (fr != rv) {
      vector<int> ff = Mp[rv];
      int siz = min(bt.size(), ff.size());
      done.insert(rv);
      for (int i = 0; i < siz; i++) {
        ans += max(0, bt[i] + ff[i]);
      }
    } else {
      long long a, b, c;
      a = b = c = 0;
      for (int i = 0; i < bt.size(); i++) {
        c += bt[i];
        if (i & 1)
          b = max(b, c);
        else
          a = max(a, c);
      }
      ans += b;
      extra = max(extra, a - b);
    }
  }
  cout << ans + extra << endl;
}
