#include<bits/stdc++.h>
using namespace std;
#define ll long long
map<pair<string,ll>,ll> mp,visit1;
ll cost[52],flag,n;
string ar[52];
pair<string,ll> compSuff(string s1,string s2)
{
	ll i,j,f=1,k;
	i=0;
	j=s2.size()-1;
	while(i<s1.size() && j>=0)
	{
		if(s1[i]!=s2[j])
		{
			f=0;
			break;
		}
		i++;
		j--;
	}
	string str="";
	if(i==s1.size())
	{
		for(k=j;k>=0;k--)
		{
			str+=s2[k];
		}
	}
	else
	{
		for(k=i;k<s1.size();k++)
		{
			str+=s1[k];
		}
	}
	pair<string,ll> p={str,f};
	return p;
}
pair<string,ll> compPref(string s1,string s2)
{
	ll i,j,k,f=1;
	i=0;
	j=0;
	while(i<s1.size() && j<s2.size())
	{
		if(s1[i]!=s2[j])
		{
			f=0;
			break;
		}
		i++;
		j++;
	}
	string str="";
	if(i==s1.size())
	{
		for(k=j;k<s2.size();k++)
		{
			str+=s2[k];
		}
	}
	else
	{
		for(k=i;k<s1.size();k++)
		{
			str+=s1[k];
		}
	}
	pair<string,ll> p={str,f};
	return p;
}
ll checkPal(string s)
{
	string str="";
	ll j;
	for(j=s.size()-1;j>=0;j--)
	{
		str+=s[j];
	}
	if(str==s)
		return 1;
	else
		return 0;
}
ll rec(string str,ll side)
{
	pair<string,ll> p= {str,side};
	ll z=checkPal(str);
	if(z)
	{
		flag=1;
		return 0;
	}
	if(mp[p]!=0)
		return mp[p];
	if(visit1[p]==1)
		return 1e17;
	visit1[p]=1;
	ll min1=1e17;
	for(ll i=1;i<=n;i++)
	{
		pair<string,ll> z;
		if(side==0)
			z=compSuff(str,ar[i]);
		else
			z=compPref(str,ar[i]);
		if(z.second==1)
		{
			if(str.size()>=ar[i].size())
				min1=min(min1,cost[i]+rec(z.first,side));
			else
				min1=min(min1,cost[i]+rec(z.first,!side));
		}
	}
	return mp[p]=min1;
}
int main()
{
	ll i,j;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>ar[i]>>cost[i];
	}
	ll min1=1e17;
	for(i=1;i<=n;i++)
	{
		min1=min(min1,cost[i]+rec(ar[i],0));
	}
	if(flag==0)
		cout<<-1;
	else
		cout<<min1;

}