#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
#define rep(i,n) for(ll i=0;i<n;i++)
#define req(x,y) for(ll j=x,k=y;j<k;j++,k--)
#define ss(x) s[x].size()
#define ch(w,x,y) if(s[b][w]!=s[x][y])
#define cm(w,x,y,z) w=min(w,c[i]+f(x,y,z))

ll n,c[51],d[51][21][2],v[51][21][2],a=1e18;
string s[51];

ll f(ll b,ll p,ll g){
	if(p==ss(b))return 0;
	ll &e=v[b][p][g];
	ll &m=d[b][p][g];
	if(e)return m;
	e=1,m=1e18;
	ll l=1;
	if(!g){req(p,ss(b)-1)ch(j,b,k)l=0;}
	else{req(0,p)ch(j,b,k)l=0;}
	if(l)return m=0;
	rep(i,n){
		ll y=1;
		ll j,k;
		if(!g){
			for(j=p,k=ss(i)-1;j<ss(b) && k>=0 && y;j++,k--) {
				ch(j,i,k)y=0;
			}
			if(y){
				if(k<0)cm(m,b,j,0);
				else cm(m,i,k,1);
			}
		}
		else{
			for(j=p,k=0;k<ss(i) && j>=0 && y;j--,k++){
				ch(j,i,k)y=0;
			}
			if(y){
				if(j<0)cm(m,i,k,0);
				else cm(m,b,j,1);
			}
		}
	}
	return m;
}

int main() {
	cin>>n;
	rep(i,n)cin>>s[i]>>c[i];
	rep(i,n)cm(a,i,0,0);
	cout<<(a==1e18 ? -1 : a)<<endl;
}
