#include <bits/stdc++.h>
using namespace std;
int getBeautyForPalind(vector<int> &bv, int &odd_add_beauty) {
  int beauty = 0;
  int i = 1;
  for (; i < bv.size(); i += 2) {
    int b = bv[i - 1] + bv[i];
    if (b > 0) {
      beauty += b;
    } else {
      break;
    }
  }
  if (i - 2 >= 0 and bv[i - 2] < 0) {
    odd_add_beauty = 0 - bv[i - 2];
  } else if ((i - 1) < bv.size()) {
    odd_add_beauty = bv[i - 1];
  } else {
    odd_add_beauty = 0;
  }
  return beauty;
}
int getBeautyForPalindPair(vector<int> &bv1, vector<int> &bv2) {
  int beauty = 0;
  for (int i = 0; i < bv1.size() and i < bv2.size(); i++) {
    int b = bv1[i] + bv2[i];
    if (b > 0) {
      beauty += b;
    } else {
      break;
    }
  }
  return beauty;
}
int main() {
  int k, n;
  while (cin >> k >> n) {
    map<string, vector<int> > pb;
    while (k-- > 0) {
      string present;
      int beauty;
      cin >> present >> beauty;
      map<string, vector<int> >::iterator it = pb.find(present);
      if (it == pb.end()) {
        vector<int> bs;
        bs.push_back(beauty);
        pb[present] = bs;
      } else {
        it->second.push_back(beauty);
      }
    }
    int beauty = 0;
    int most_diff_beauty = 0;
    for (map<string, vector<int> >::iterator it = pb.begin(); it != pb.end();
         it++) {
      string p = it->first;
      sort(it->second.begin(), it->second.end(), greater<int>());
      string p2 = p;
      if (p2.size() > 1) {
        reverse(p2.begin(), p2.end());
      }
      if (p == p2) {
        int diff_beauty;
        int b = getBeautyForPalind(it->second, diff_beauty);
        beauty += b;
        if (diff_beauty > most_diff_beauty) {
          most_diff_beauty = diff_beauty;
        }
      } else {
        if (p < p2) {
          map<string, vector<int> >::iterator it2 = pb.find(p2);
          if (it2 != pb.end()) {
            sort(it2->second.begin(), it2->second.end(), greater<int>());
            beauty += getBeautyForPalindPair(it->second, it2->second);
          }
        }
      }
    }
    beauty += most_diff_beauty;
    cout << beauty << endl;
  }
  return 0;
}
