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 *s*<sub class="lower-index">*i*</sub> having the same length *n*. We denote the beauty of the *i*-th string by *a*<sub class="lower-index">*i*</sub>. It can happen that *a*<sub class="lower-index">*i*</sub> 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 *a*<sub class="lower-index">*i*</sub>'s are negative, Santa can obtain the empty string.
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 *s*<sub class="lower-index">*i*</sub> and its beauty *a*<sub class="lower-index">*i*</sub> ( - 10 000 ≤ *a*<sub class="lower-index">*i*</sub> ≤ 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.
In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order).
7 3
abb 2
aaa -3
bba -1
zyz -4
abb 5
aaa 7
xyx 4
12
3 1
a 1
a 2
a 3
6
2 5
abcde 10000
abcde 10000
0
10 10
njxbzflaka -1
felbvvtkja 6
gxiuztqkcw 5
aomvscmtti 6
jsqmkoyuca -2
wckqtzuixg 5
ajktvvblef -5
ittmcsvmoa -1
akalfzbxjn 10
acuyokmqsj 8
31
10 20
jvyxocgomfmrtllgmagp 13
gvtjnyaofrswcnnifzfq 17
qisxpseggpjfoijmqnel -5
lenqmjiofjpggespxsiq 14
qfzfinncwsrfoaynjtvg 14
caayidazlylxyisihdhx 14
pgamglltrmfmogcoxyvj 11
xhdhisiyxlylzadiyaac 2
tbirihfpjgbbtclpxwhv 19
vhwxplctbbgjpfhiribt 10
109
1 1
e -1
0
2 1
t 1
t 2
3
1 2
yy 1
1
2 2
sn 1
ns 2
3
3 3
ada -1
xuv -1
vux 3
2
4 3
dbd 24
aba 90
cbc 54
aba 46
190
2 3
aaa 5
aaa -2
5
4 3
aba 4
aba 3
aba 3
aba -2
10
4 3
aba 4
aba 2
aba 2
aba -1
8
3 2
aa 5
aa -2
bb 1
5
2 2
aa 500
aa -50
500
2 1
a 5
a -1
5
2 3
aba 10
aba -3
10
2 3
aba 10
aba -9
10
import sys
input = sys.stdin.readline

done, vals, pv = {}, {}, []

k, n = map(int, input().split())
v, pals = [], []

for i in range(k):
    s, a = input().split()
    if s not in vals : vals[s] = []
    vals[s].append(int(a))
    rev = s[::-1]
    if rev not in vals : vals[rev] = []
    if rev != s : v.append(rev)
    else : pals.append(rev)

for s in v : vals[s].sort(reverse=True)

for s in pals:
    if s in done : continue
    vals[s].sort(reverse=True)
    pv.append(vals[s])
    done[s] = 1

ans, tot_even = 0, 0

for s in v:
    if s in done : continue
    rev = s[::-1]
    if rev not in vals : continue
    x, y = vals[s], vals[rev]
    for i in range(min(len(x), len(y))):
        ans += max(x[i]+y[i], 0)
    done[s] = done[rev] = 1

sum = [[0]*2 for i in range(len(pv))]

for i in range(len(pv)):
    cur = 0
    for j in range(len(pv[i])):
        cur += pv[i][j]
        sum[i][j%2] = max(sum[i][j%2], cur)
    tot_even += sum[i][1]

mx = tot_even
if len(pv) > 0 : mx = max(mx, max([tot_even - sum[i][1] + sum[i][0] for i in range(len(pv))]))
print(ans+mx)