#include <bits/stdc++.h>
using namespace std;
int k, l, score, neg = 0, pos = 0, visited[100001];
long long ans;
vector<int> v[100001];
map<string, int> m;
string s;
bool comp(int a, int b) { return a > b; }
string rev(string s) {
  int n = s.length();
  for (int i = 0; i < n / 2; i++) {
    char tmp = s[i];
    s[i] = s[n - i - 1];
    s[n - i - 1] = tmp;
  }
  return s;
}
void findans() {
  map<string, int>::iterator it;
  for (it = m.begin(); it != m.end(); it++) {
    if (visited[m[it->first]]) continue;
    string r = rev(it->first);
    int t1 = m[it->first];
    if (m[r] == 0) continue;
    int t2 = m[r];
    visited[t1] = visited[t2] = 1;
    if (r != it->first) {
      int i = 0, j = 0;
      while (i < v[t1].size() && j < v[t2].size() && v[t1][i] + v[t2][j] > 0) {
        ans += v[t1][i++] + v[t2][j++];
      }
    } else {
      int n = v[t1].size();
      int i = 0;
      for (; i < n - 1; i += 2) {
        if (v[t1][i] + v[t1][i + 1] > 0) {
          ans += v[t1][i] + v[t1][i + 1];
          pos = max(pos, -v[t1][i + 1]);
        } else
          break;
      }
      if (i < n) pos = max(pos, v[t1][i]);
    }
  }
  cout << ans + pos;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> k >> l;
  int j = 1;
  for (int i = 0; i < k; i++) {
    cin >> s >> score;
    if (m[s] == 0) m[s] = j++;
    v[m[s]].push_back(score);
  }
  for (int i = 0; i < 100001; i++) {
    if (v[i].size()) {
      sort(v[i].begin(), v[i].end(), comp);
    }
  }
  findans();
}
