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.*;
import java.util.*;
public final class santa_builds_a_palindrome
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static FastScanner sc=new FastScanner(br);
    static PrintWriter out=new PrintWriter(System.out);
	static Random rnd=new Random();
	static Map<String,ArrayList<Integer>> m1=new HashMap<>(),m2=new HashMap<>();
	
	static void put1(String s,int cost)
	{
		if(m1.get(s)==null)
		{
			ArrayList<Integer> al=new ArrayList<Integer>();al.add(cost);m1.put(s,al);
		}
		else
		{
			ArrayList<Integer> al=m1.get(s);al.add(cost);m1.put(s,al);
		}
	}
	
	static void put2(String s,int cost)
	{
		if(m2.get(s)==null)
		{
			ArrayList<Integer> al=new ArrayList<Integer>();al.add(cost);m2.put(s,al);
		}
		else
		{
			ArrayList<Integer> al=m2.get(s);al.add(cost);m2.put(s,al);
		}
	}
	
	static boolean isPal(String s)
	{
		int n=s.length();
		for(int i=0,j=n-1;i<n/2;i++,j--)
		{
			if(s.charAt(i)!=s.charAt(j))
			{
				return false;
			}
		}
		return true;
	}
	
	static String reverse(String curr)
	{
		StringBuffer sb=new StringBuffer(curr);
		return new String(sb.reverse().toString());
	}
	
    public static void main(String args[]) throws Exception
    {
		int n=sc.nextInt(),k=sc.nextInt();
		for(int i=0;i<n;i++)
		{
			String s=sc.next();int cost=sc.nextInt();
			if(!isPal(s))
			{
				put1(s,cost);
			}
			else
			{
				put2(s,cost);
			}
		}
		long ans=0;
		for(Map.Entry<String,ArrayList<Integer>> en:m1.entrySet())
		{
			ArrayList<Integer> al1=en.getValue();String curr=en.getKey(),rev=reverse(curr);ArrayList<Integer> al2=m1.get(rev);
			if(al2!=null)
			{
				Collections.sort(al1,Collections.reverseOrder());Collections.sort(al2,Collections.reverseOrder());
				for(int i=0;i<Math.min(al1.size(),al2.size());i++)
				{
					if(al1.get(i)+al2.get(i)>=0)
					{
						ans+=al1.get(i)+al2.get(i);
					}
				}
			}
		}
		ans/=2;
		long sum=0;List<Pair> list=new ArrayList<Pair>();
		for(Map.Entry<String,ArrayList<Integer>> en:m2.entrySet())
		{
			ArrayList<Integer> al=en.getValue();Collections.sort(al,Collections.reverseOrder());
			for(int i=0;i<al.size();)
			{
				if(i+1<al.size() && al.get(i)>=0 && al.get(i+1)>=0)
				{
					ans+=al.get(i)+al.get(i+1);i+=2;continue;
				}
				else if(i+1<al.size() && al.get(i)+al.get(i+1)>=0)
				{
					list.add(new Pair(al.get(i),al.get(i)+al.get(i+1)));sum+=al.get(i)+al.get(i+1);
					break;
				}
				else if(al.get(i)>=0)
				{
					list.add(new Pair(al.get(i),0));
					break;
				}
				else
				{
					break;
				}
			}
		}
		list.add(new Pair(0,0));long max=ans;
		for(int i=0;i<list.size();i++)
		{
			long curr1=list.get(i).a,curr2=list.get(i).b;
			max=Math.max(max,ans+(sum-curr2)+curr1);
		}
		out.println(max);out.close();
    }
}
class Pair
{
	long a,b;
	public Pair(long a,long b)
	{
		this.a=a;
		this.b=b;
	}
}
class FastScanner
{
    BufferedReader in;
    StringTokenizer st;

    public FastScanner(BufferedReader in) {
        this.in = in;
    }
	
    public String nextToken() throws Exception {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }
	
	public String next() throws Exception {
		return nextToken().toString();
	}
	
    public int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() throws Exception {
        return Long.parseLong(nextToken());
    }

    public double nextDouble() throws Exception {
        return Double.parseDouble(nextToken());
    }
}