#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
const long long MOD = 1000000007ll;
const long double PI = acos(-1.0);
const long long INF = 2000000000000000000ll;
const long double EPS = 1e-9;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
template <typename T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
template <typename T>
T lcm(T a, T b) {
  return a * (b / gcd(a, b));
}
template <typename T>
T sqr(T a) {
  return a * a;
}
bool pal(string &x) {
  int n = x.length();
  for (int i = 0; i < n / 2; i++) {
    if (x[i] != x[n - i - 1]) return 0;
  }
  return 1;
}
map<string, vector<int> > m;
string rev(string x) {
  int n = x.length();
  for (int i = 0; i < n / 2; i++) {
    swap(x[i], x[n - i - 1]);
  }
  return x;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, len;
  cin >> n >> len;
  string s;
  int foo;
  for (int i = 0; i < n; i++) {
    cin >> s >> foo;
    m[s].push_back(foo);
  }
  long long bigsum = 0;
  int max_pal = 0;
  for (auto x : m) {
    string a = x.first;
    string b = rev(a);
    if (a > b) continue;
    if (a == b) {
      int last = inf;
      int sum = 0;
      int k = 0;
      int max_minus = -inf;
      for (auto y : x.second) {
        if (y > 0) {
          k++;
          last = min(last, y);
          sum += y;
        } else
          max_minus = max(max_minus, y);
      }
      if (k % 2 == 1 && last != inf) {
        if (max_minus + last > 0) {
          sum += max_minus;
          max_pal = max(max_pal, -max_minus);
        } else {
          sum -= last;
          max_pal = max(max_pal, last);
        }
      }
      bigsum += sum;
    } else {
      vector<int> v = x.second;
      vector<int> u = m[b];
      sort((v).begin(), (v).end(), greater<int>());
      sort((u).begin(), (u).end(), greater<int>());
      int f = min(u.size(), v.size());
      int sum = 0;
      for (int i = 0; i < f; i++) {
        if (u[i] + v[i] > 0)
          sum += u[i] + v[i];
        else
          break;
      }
      bigsum += sum;
    }
  }
  cout << bigsum + max_pal;
  return 0;
}
