#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int n, s;
  cin >> n >> s;
  map<string, priority_queue<int> > MAP;
  map<string, priority_queue<int> >::iterator iter;
  for (int i = 0; i < n; i++) {
    int x;
    string s;
    cin >> s >> x;
    iter = MAP.find(s);
    if (iter == MAP.end()) {
      priority_queue<int> Q;
      Q.push(x);
      MAP.insert(make_pair(s, Q));
    } else {
      iter->second.push(x);
    }
  }
  long long int total = 0;
  map<string, priority_queue<int> >::iterator Reverse;
  int max = -100000000;
  for (map<string, priority_queue<int> >::iterator forward = MAP.begin();
       forward != MAP.end(); ++forward) {
    string str = forward->first;
    reverse(str.begin(), str.end());
    Reverse = MAP.find(str);
    if (forward == Reverse) {
      priority_queue<int> &Q = forward->second;
      while (Q.size() >= 2) {
        int a = Q.top();
        Q.pop();
        int b = Q.top();
        Q.pop();
        if (a + b <= 0) {
          if (a > max) {
            max = a;
          }
          break;
        }
        if (b >= 0) {
          total += (a + b);
        } else if (a > 0) {
          total += (a + b);
          if (max < (-1 * b)) {
            max = -1 * b;
          }
          break;
        }
      }
      if (Q.size() == 1) {
        int a = Q.top();
        if (a > max) max = a;
      }
    } else if (Reverse != MAP.end()) {
      priority_queue<int> &Qf = forward->second;
      priority_queue<int> &Qr = Reverse->second;
      while (!Qf.empty() && !Qr.empty()) {
        int f = Qf.top();
        int r = Qr.top();
        if (f + r <= 0) {
          break;
        }
        total += (f + r);
        Qf.pop();
        Qr.pop();
      }
    }
  }
  if (max > 0) total += max;
  cout << total << '\n';
}
