#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int k, n;
string str[maxn];
unsigned long long Ha1[maxn], Ha2[maxn];
int w[maxn];
unsigned long long gethash1(string &A) {
  unsigned long long res = 233;
  for (int i = 0; i < n; ++i)
    res = (res * 173323 + A[i] + 987) << 2 ^ (res >> 5);
  return res;
}
unsigned long long gethash2(string &A) {
  unsigned long long res = 233;
  for (int i = n - 1; i >= 0; --i)
    res = (res * 173323 + A[i] + 987) << 2 ^ (res >> 5);
  return res;
}
unsigned long long Ha[maxn][2];
vector<int> a[maxn];
map<unsigned long long, int> dict;
int tot = 0;
map<unsigned long long, int> dict2;
long long work1() {
  long long ans = 0;
  for (int i = 1; i <= tot; ++i) {
    if (Ha[i][0] == Ha[i][1]) {
      int SZ = a[i].size();
      for (int j = SZ - 1; j >= 1 && a[i][j] + a[i][j - 1] > 0; j -= 2) {
        ans += a[i][j] + a[i][j - 1];
      }
    } else if (Ha[i][0] < Ha[i][1]) {
      int t = dict[Ha[i][1]];
      int SZ1 = a[i].size(), SZ2 = a[t].size();
      for (int j = 0;
           j < SZ1 && j < SZ2 && a[i][SZ1 - j - 1] + a[t][SZ2 - j - 1] > 0;
           ++j) {
        ans = ans + a[i][SZ1 - j - 1] + a[t][SZ2 - j - 1];
      }
    }
  }
  return ans;
}
long long work2() {
  long long ans = 0;
  long long maxdelta = 0;
  for (int i = 1; i <= tot; ++i) {
    if (Ha[i][0] == Ha[i][1]) {
      int SZ = a[i].size();
      if (SZ == 1 && a[i][0] > maxdelta) maxdelta = a[i][0];
      if (SZ >= 2 && a[i][SZ - 1] > maxdelta &&
          a[i][SZ - 1] + a[i][SZ - 2] <= 0)
        maxdelta = a[i][SZ - 1];
      for (int j = SZ - 1; j >= 1 && a[i][j] + a[i][j - 1] > 0; j -= 2) {
        ans += a[i][j] + a[i][j - 1];
        if (a[i][j - 1] < 0 && -a[i][j - 1] > maxdelta) {
          maxdelta = -a[i][j - 1];
        }
        if (j >= 3 && a[i][j - 2] > maxdelta && a[i][j - 2] + a[i][j - 3] <= 0)
          maxdelta = a[i][j - 2];
        if (j == 2 && a[i][0] > maxdelta) maxdelta = a[i][0];
      }
    } else if (Ha[i][0] < Ha[i][1]) {
      int t = dict[Ha[i][1]];
      int SZ1 = a[i].size(), SZ2 = a[t].size();
      for (int j = 0;
           j < SZ1 && j < SZ2 && a[i][SZ1 - j - 1] + a[t][SZ2 - j - 1] > 0;
           ++j) {
        ans = ans + a[i][SZ1 - j - 1] + a[t][SZ2 - j - 1];
      }
    }
  }
  return ans + maxdelta;
}
int main() {
  cin >> k >> n;
  for (int i = 1; i <= k; ++i) cin >> str[i] >> w[i];
  int t;
  for (int i = 1; i <= k; ++i) {
    Ha1[i] = gethash1(str[i]);
    Ha2[i] = gethash2(str[i]);
    if (Ha1[i] == Ha2[i]) {
      if (dict2[Ha1[i]]) {
        t = dict2[Ha1[i]];
      } else {
        dict2[Ha1[i]] = t = ++tot;
        Ha[tot][0] = Ha[tot][1] = Ha1[i];
      }
      a[t].push_back(w[i]);
    } else {
      if (dict[Ha1[i]]) {
        t = dict[Ha1[i]];
      } else {
        dict[Ha1[i]] = t = ++tot;
        Ha[tot][0] = Ha1[i];
        Ha[tot][1] = Ha2[i];
      }
      a[t].push_back(w[i]);
    }
  }
  long long ans = 0;
  for (int i = 1; i <= tot; ++i) sort(a[i].begin(), a[i].end());
  printf("%lld\n", max(work1(), work2()));
  return 0;
}
