Santa Claus likes palindromes very much. There was his birthday recently. k of his friends came to him to congratulate him, and each of them presented to him a string si having the same length n. We denote the beauty of the i-th string by ai. It can happen that ai is negative — that means that Santa doesn't find this string beautiful at all.

Santa Claus is crazy about palindromes. He is thinking about the following question: what is the maximum possible total beauty of a palindrome which can be obtained by concatenating some (possibly all) of the strings he has? Each present can be used at most once. Note that all strings have the same length n.

Recall that a palindrome is a string that doesn't change after one reverses it.

Since the empty string is a palindrome too, the answer can't be negative. Even if all ai's are negative, Santa can obtain the empty string.

Input

The first line contains two positive integers k and n divided by space and denoting the number of Santa friends and the length of every string they've presented, respectively (1 ≤ k, n ≤ 100 000; n·k ≤ 100 000).

k lines follow. The i-th of them contains the string si and its beauty ai ( - 10 000 ≤ ai ≤ 10 000). The string consists of n lowercase English letters, and its beauty is integer. Some of strings may coincide. Also, equal strings can have different beauties.

Output

In the only line print the required maximum possible beauty.

Examples

Input

7 3
abb 2
aaa -3
bba -1
zyz -4
abb 5
aaa 7
xyx 4


Output

12


Input

3 1
a 1
a 2
a 3


Output

6


Input

2 5
abcde 10000
abcde 10000


Output

0

Note

In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order).
#include <bits/stdc++.h>
using namespace std;
const long long Nmax = 1e5 + 5;
string s[Nmax];
vector<long long> mx[Nmax];
long long a[Nmax];
long long p[Nmax][26];
long long id[Nmax];
long long n, k, ans, N;
bool flag[Nmax];
bool pali[Nmax];
bool palin(long long id) {
  for (long long i = 0, j = k - 1; i < j; i++, j--) {
    if (s[id][i] != s[id][j]) return false;
  }
  return true;
}
bool cmp(long long i, long long j) { return a[i] < a[j]; }
void build_trie() {
  for (long long i = 1; i <= n; i++) {
    long long pos = 0;
    for (long long j = 0; j < k; j++) {
      long long c = s[i][j] - 'a';
      if (p[pos][c] == 0) p[pos][c] = ++N;
      pos = p[pos][c];
      if (j + 1 == k) {
        mx[pos].push_back(i);
      }
    }
  }
  for (long long i = 0; i <= N; i++) {
    sort(mx[i].begin(), mx[i].end(), cmp);
  }
}
signed main() {
  cin >> n >> k;
  for (long long i = 1; i <= n; i++) {
    cin >> s[i] >> a[i];
    id[i] = i;
  }
  for (long long i = 1; i <= n; i++) {
    pali[i] = palin(i);
  }
  build_trie();
  sort(id + 1, id + n + 1, cmp);
  reverse(id + 1, id + n + 1);
  long long maxx = 0;
  for (long long i = 1; i <= n; i++) {
    if (flag[id[i]]) continue;
    long long u = id[i];
    reverse(s[u].begin(), s[u].end());
    long long pos = 0;
    for (long long j = 0; j < k; j++) {
      long long c = s[u][j] - 'a';
      if (!p[pos][c]) break;
      pos = p[pos][c];
      if (j + 1 == k) {
        long long m = mx[pos].size();
        m--;
        while (m >= 0 && flag[mx[pos][m]]) {
          m--;
          mx[pos].pop_back();
        }
        if (m >= 0) {
          long long v = mx[pos][m];
          if (u != v && a[u] + a[v] > 0) {
            ans += a[u] + a[v];
            flag[u] = true;
            flag[v] = true;
            mx[pos].pop_back();
            if (pali[u]) {
              maxx = max(maxx, max(a[u], a[v]) - (a[u] + a[v]));
            }
          }
        }
      }
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (flag[i]) continue;
    if (palin(i)) maxx = max(maxx, a[i]);
  }
  cout << ans + maxx;
  return 0;
}
