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).
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.InputMismatchException;

public class C6{

	void solve()
	{
		int n = ni();
		int K = ni();
		HashMap<String,ArrayList<Integer>> pal = new HashMap<>();
		HashMap<String,ArrayList<Integer>> npal = new HashMap<>();
		for(int i=0;i<n;i++)
		{
			String s = ns();
			int val = ni();
			if(isPal(s))
			{
				if(!pal.containsKey(s))
					pal.put(s,new ArrayList<>());
				ArrayList<Integer> cur = pal.get(s);
				cur.add(val);
				pal.put(s,cur);
			}
			else
			{
				if(!npal.containsKey(s))
					npal.put(s,new ArrayList<>());
				ArrayList<Integer> cur = npal.get(s);
				cur.add(val);
				npal.put(s,cur);
			}
		}
		long ans = 0;
		HashMap<String,ArrayList<Integer>> npal2 = new HashMap<>(npal);
		for(String str : npal.keySet())
		{
			if(npal.containsKey(str) && npal2.containsKey(rev(str)))
			{
				ArrayList<Integer> l1 = npal2.get(str);
				ArrayList<Integer> l2 = npal2.get(rev(str));
				Collections.sort(l1,Collections.reverseOrder());
				Collections.sort(l2,Collections.reverseOrder());
				int lim = Math.min(l1.size(),l2.size());
				for(int i=0;i<lim;i++)
				{
					int k1 = l1.get(i);
					int k2 = l2.get(i);
					if(k1+k2>0)
						ans += (k1+k2);
					else
						break;
				}
				npal2.remove(str);
				npal2.remove(rev(str));
			}
		}
		int fin = 0;
		if(pal.size()!=0)
		{
			int[][] store = new int[pal.size()][2];
			int ptr = 0;
			for(int i=0;i<store.length;i++)
				store[i][0] = store[i][1] = Integer.MIN_VALUE;
			for(String str : pal.keySet())
			{
				ArrayList<Integer> list = pal.get(str);
				Collections.sort(list,Collections.reverseOrder());
				int i=0;
				for(	;i<list.size()-1;i+=2)
				{
					int k1 = list.get(i);
					int k2 = list.get(i+1);
					if(k1<=0)
						break;
					if(k2>0)
						ans += (k1+k2);
					else
					{
						if(k1+k2<0)
							store[ptr][0] = k1;
						else
						{
							store[ptr][0] = k1;
							store[ptr][1] = k2;
						}
						break;
					}
				}
				if(list.size()%2==1 && list.get(list.size()-1)>0)
					store[ptr][0] = list.get(list.size()-1);
				ptr++;
			}
			
			int sng = 0;
			int dbl = Integer.MAX_VALUE;
			for(int i=0;i<ptr;i++)
			{
				if(store[i][0]==Integer.MIN_VALUE)
					continue;
				if(store[i][1]==Integer.MIN_VALUE)
					sng = Math.max(sng, store[i][0]);
				else
				{
					ans += store[i][0] + store[i][1];
					dbl = Math.min(dbl,store[i][1]);
				}
			}
			fin = Math.max(sng,-dbl);
		}
		out.println(ans + fin);
	}
	
	boolean isPal(String s)
	{
		char[] c = s.toCharArray();
		for(int i=0;i<c.length/2;i++)
			if(c[i]!=c[c.length-i-1])
				return false;
		return true;
	}
	
	String rev(String s)
	{
		char[] s2 = s.toCharArray();
		char[] s1 = new char[s2.length];
		for(int i=0;i<s2.length;i++)
			s1[i] = s2[s2.length-i-1];
		return new String(s1);
	}
	
	public static void main(String[] args){new C6().start();}
	
	private byte[] bufferArray = new byte[1024];
	private int bufLength = 0;
	private int bufCurrent = 0;
	InputStream inputStream;
	PrintWriter out;
	
	void start()
	{
		inputStream = System.in;
		out = new PrintWriter(System.out);
		solve();
		out.flush();
	}
	
	int nextByte()
	{
		if(bufLength==-1)
			throw new InputMismatchException();
		if(bufCurrent>=bufLength)
		{
			bufCurrent = 0;
			try
			{bufLength = inputStream.read(bufferArray);}
			catch(IOException e)
			{ throw new InputMismatchException();}
			if(bufLength<=0)
				return -1;
		}
		return bufferArray[bufCurrent++];
	}
	
	boolean isSpaceChar(int x)
	{return (x<33 || x>126);}
	
	boolean isDigit(int x)
	{return (x>='0' && x<='9');}
	
	int nextNonSpace()
	{
		int x;
		while((x=nextByte())!=-1 && isSpaceChar(x));
		return x;
	}
	
	int ni()
	{
		int ans = 0;
		int sign = 1;
		int x = nextNonSpace();
		if(x=='-') 
		{
			sign = -1;
			x = nextByte();
		}
		while(!isSpaceChar(x))
		{
			if(isDigit(x))
			{
				ans = ans*10 + x -'0';
				x = nextByte();
			}
			else
				throw new InputMismatchException();
		}
		return sign*ans;
	}
	
	String ns()
	{
		StringBuilder sb = new StringBuilder();
		int x = nextNonSpace();
		while(!isSpaceChar(x))
		{
			sb.append((char)x);
			x = nextByte();
		}
		return sb.toString();
	}
	
}
