#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef pair<pli,pii> plii;
#define mkp make_pair
#define fi first
#define se second
int n;
ll dis[53][21][2];
char c[53][23];
int len[53],C[53];
priority_queue<plii,vector<plii>,greater<plii> >pq;
void add(ll a,int t,int x,int y){
	pq.push(mkp(mkp(a,t),mkp(x,y)));
} 
char cur[104];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++)scanf("%s",c[i]+1),len[i]=strlen(c[i]+1),cin>>C[i];
	for(int i=0;i<53;i++)for(int j=0;j<21;j++)for(int k=0;k<2;k++)dis[i][j][k]=1e18;
	ll ans=1e18;
	for(int i=1;i<=n;i++){
		dis[i][1][0]=dis[i][len[i]][1]=C[i];
		add(C[i],i,1,0),add(C[i],i,len[i],1);
		bool ok=0;
		for(int j=1,k=len[i];j<k;j++,k--)if(c[i][j]!=c[i][k])ok=1;
		if(!ok)ans=min(ans,0ll+C[i]);
	}
	while(!pq.empty()){
		plii u=pq.top();pq.pop();
		ll w=u.fi.fi;int x=u.fi.se,a=u.se.fi,b=u.se.se;
		if(w>dis[x][a][b]||w>=ans)continue;
		if(b==0){
			for(int i=1;i<=n;i++){
				int cnt=0;
				for(int j=a;j<=len[x];j++)cur[++cnt]=c[x][j];
				for(int j=1;j<=len[i];j++)cur[++cnt]=c[i][j];
				bool ok=0;
				for(int j=1,k=cnt;j<k;j++,k--)if(cur[j]!=cur[k])ok=1;
				if(!ok)ans=min(ans,w+C[i]);
			}
			for(int i=1;i<=n;i++){
				bool ok=0;
				for(int j=a,k=len[i];j<=len[x]&&k>=1;j++,k--)if(c[x][j]!=c[i][k])ok=1;
				if(ok)continue;
				ll val=w+C[i];
				int lt=len[x]-a+1,AB=abs(lt-len[i]);
				if(lt<len[i]&&dis[i][AB][1]>val)dis[i][AB][1]=val,add(val,i,AB,1);
				if(lt>len[i]&&dis[x][len[x]-AB+1][0]>val)dis[x][len[x]-AB+1][0]=val,add(val,x,len[x]-AB+1,0);
			}
		}else{
			for(int i=1;i<=n;i++){
				int cnt=0;
				for(int j=1;j<=len[i];j++)cur[++cnt]=c[i][j];
				for(int j=1;j<=a;j++)cur[++cnt]=c[x][j];
				bool ok=0;
				for(int j=1,k=cnt;j<k;j++,k--)if(cur[j]!=cur[k])ok=1;
				if(!ok)ans=min(ans,w+C[i]);
			}
			for(int i=1;i<=n;i++){
				bool ok=0;
				for(int j=a,k=1;j>=1&&k<=len[i];j--,k++)if(c[x][j]!=c[i][k])ok=1;
				if(ok)continue;
				ll val=w+C[i];
				int AB=abs(a-len[i]);
				if(a<len[i]&&dis[i][len[i]-AB+1][0]>val)dis[i][len[i]-AB+1][0]=val,add(val,i,len[i]-AB+1,0);
				if(a>len[i]&&dis[x][AB][1]>val)dis[x][AB][1]=val,add(val,x,AB,1);
			}
		}
	}
	if(ans==1e18)puts("-1");
	else cout<<ans;
}