/*
{

AuThOr Gwj
*/
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define R(a) cin>>a
#define R2(a,b) cin>>a>>b
#define int LL

using namespace std;
const LL INF=10000000000000000;
typedef pair<int,int> mp;
/*}
*/
int f[51][2][21];
string s[51];
int c[51];
bool check(int index,int l,int r){
	rb(i,l,r)
	{
		if(s[index][i]==s[index][r-(i-l)]);
		else{
			return false;
		}
	}
	return true;
}
bool equal(int index1,int l1,int r1,int index2,int l2,int r2){
	rl(i,r1,l1){
		if(s[index1][i]!=s[index2][l2+(r1-i)])  return false;
	}
	return 1;
}
#define VAL FIR 
#define A SEC.FIR.FIR
#define B SEC.FIR.SEC
#define C SEC.SEC
#define TY pair<int,pair<mp,int> >
signed main(){
	fastio;
	int n;
	R(n);
	rb(i,1,n)
		R2(s[i],c[i]);
	priority_queue<TY,vector<TY> ,greater<TY> > q;
	memset(f,127,sizeof(f));
	rb(i,1,n){
		f[i][0][s[i].length()]=c[i];
		q.push(II(c[i],II(II(i,0),s[i].length())));
	}
	rb(i,1,n)
		s[i]='$'+s[i]; 
	int res=INF;
	while(!q.empty()){
		int i,j,k;
		i=(q.top()).A;
		j=(q.top()).B;
		k=(q.top()).C;
		if(q.top().VAL!=f[i][j][k]){
			q.pop();
			continue;
		}
		q.pop();
			if(!j){
				rb(l,1,n)
				{
					if(s[l].length()-1==k){
						if(equal(i,s[i].length()-k,s[i].length()-1,l,1,s[l].length()-1)){
							res=min(res,f[i][j][k]+c[l]);
						}
					}
					else{
						if(s[l].length()-1<k){
							if(equal(i,s[i].length()-k,s[i].length()-k-1+s[l].length()-1,l,1,s[l].length()-1)){
								if(f[i][j][k-s[l].length()+1]>f[i][j][k]+c[l]){
									f[i][j][k-s[l].length()+1]=f[i][j][k]+c[l];
									q.push(II(f[i][j][k-s[l].length()+1],II(II(i,j),k-s[l].length()+1)));
								}
							}
						}
						else{//
							if(equal(i,s[i].length()-k,s[i].length()-1,l,s[l].length()-k,s[l].length()-1)){				
								if(f[l][j^1][s[l].length()-1-k]>f[i][j][k]+c[l]){
										f[l][j^1][s[l].length()-1-k]=f[i][j][k]+c[l];
										q.push(II(f[l][j^1][s[l].length()-1-k],II(II(l,j^1),s[l].length()-1-k)));
								}
							}
						}
					}
					}	
			}
			else{
				rb(l,1,n)
				{
				if(s[l].length()-1==k){
					if(equal(i,1,k,l,1,s[l].length()-1)){
						res=min(res,f[i][j][k]+c[l]);
					}
				}
				else{
					if(s[l].length()-1<k){
						if(equal(i,k-(s[l].length()-1)+1,k,l,1,s[l].length()-1)){
							if(f[i][j][k-s[l].length()+1]>f[i][j][k]+c[l]){
								f[i][j][k-s[l].length()+1]=f[i][j][k]+c[l];
								q.push(II(f[i][j][k-s[l].length()+1],II(II(i,j),k-s[l].length()+1)));
							}
						}	
					}
					else{
						if(equal(i,1,k,l,1,k)){
							if(f[l][j^1][s[l].length()-1-k]>f[i][j][k]+c[l]){
								f[l][j^1][s[l].length()-1-k]=f[i][j][k]+c[l];
								q.push(II(f[l][j^1][s[l].length()-1-k],II(II(l,j^1),s[l].length()-1-k)));
							}
						}
					}
				}
			}	
		}
	}
	rb(i,1,n)
		rep(j,2)
			rb(k,1,s[i].length()-1){
				if(j){
					if(check(i,1,k)){
						res=min(res,f[i][j][k]);
					}
				}
			 	else{
					if(check(i,s[i].length()-k,s[i].length()-1)){
						res=min(res,f[i][j][k]);
					}
				}
			}
	if(res>=INF) res=-1;
	assert(res!=INF);
	cout<<res<<endl;
	return 0;
}

	

Measured memory_bytes: 3684