#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#define SF scanf
#define PF printf
#define MAXN 55
#define MAXL 22
#define MAXM 2010
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n;
char s[MAXN][MAXL];
bool used[MAXM];
int idx[2][MAXN][MAXL],len[MAXN],cnt;
ll c[MAXN],vis[MAXM],ans=INF;
vector<int>a[MAXM];
vector<ll>w[MAXM];
queue<int> q;
int calc(int x,int pos,int y,int tag){
	int totl=0;
	if(tag==0){
		while(pos+totl<len[x]&&totl<len[y]&&s[x][pos+totl]==s[y][len[y]-1-totl])
			totl++;
		if(totl==0)
			return -1;
		if(pos+totl==len[x]&&totl==len[y])
			return 0;
		if(pos+totl==len[x]&&totl<len[y])
			return idx[1][y][len[y]-1-totl];
		if(pos+totl<len[x]&&totl==len[y])
			return idx[0][x][pos+totl];
	}
	else{
		while(pos-totl>=0&&totl<len[y]&&s[x][pos-totl]==s[y][totl])
			totl++;
		if(totl==0)
			return -1;
		if(pos<totl&&totl==len[y])
			return 0;
		if(pos<totl&&totl<len[y])
			return idx[0][y][totl];
		if(pos>=totl&&totl==len[y])
			return idx[1][x][pos-totl];
	}
	return -1;
}
ll spfa(){
	while(!q.empty()){
		int x=q.front();
		q.pop();
		used[x]=0;
//		PF("{%d %lld}\n",x,vis[x]);
		for(int i=0;i<a[x].size();i++)
			if(vis[a[x][i]]==-1||vis[a[x][i]]>vis[x]+w[x][i]){
				vis[a[x][i]]=vis[x]+w[x][i];	
				if(used[a[x][i]]==0){
					used[a[x][i]]=1;
					q.push(a[x][i]);
				}
			}
	}
	if(ans!=0x3f3f3f3f&&(vis[0]==-1||vis[0]>ans))
		vis[0]=ans;
	return vis[0];
}
int check1(int x,int pos){
	int lens=0;
	while(pos-lens>0&&pos+lens<len[x]-1&&s[x][pos-lens-1]==s[x][pos+lens+1])
		lens++;
	return lens;
}
int check2(int x,int pos){
	int lens=0;
	while(pos-lens>0&&pos+lens+1<len[x]-1&&s[x][pos-lens-1]==s[x][pos+lens+2])
		lens++;
	return lens;	
}
int main(){
	SF("%d",&n);
	for(int i=1;i<=n;i++){
		SF("%s",s[i]);
		SF("%lld",&c[i]);
		len[i]=strlen(s[i]);
	}
	for(int i=1;i<=n;i++)
		for(int j=0;j<len[i];j++){
			idx[0][i][j]=++cnt;
			idx[1][i][j]=++cnt;
		}
	for(int i=1;i<=n;i++)
		for(int j=0;j<len[i];j++)
			for(int k=1;k<=n;k++){
				int newp1=calc(i,j,k,0);
				if(newp1>=0){
					a[idx[0][i][j]].push_back(newp1);
					w[idx[0][i][j]].push_back(c[k]);
				}
				int newp2=calc(i,j,k,1);
				if(newp2>=0){
					a[idx[1][i][j]].push_back(newp2);
					w[idx[1][i][j]].push_back(c[k]);
				}
			}
//	for(int i=1;i<=n;i++)
//		for(int j=0;j<len[i];j++){
//			PF("[%d %d -> %d,%d]\n",i,j,idx[0][i][j],idx[1][i][j]);	
//		}
	memset(vis,-1,sizeof vis);
	memset(used,0,sizeof used);
	for(int i=1;i<=n;i++){
		vis[idx[0][i][0]]=c[i];
		q.push(idx[0][i][0]);
		used[idx[0][i][0]]=1;
	}
	for(int i=1;i<=n;i++)
		for(int j=0;j<len[i];j++){
			int len1=check1(i,j);
			if(j-len1==0&&j+len1==len[i]-1)
				ans=min(ans,c[i]);
			else if(j-len1==0){
				vis[idx[0][i][j+len1+1]]=c[i];
				q.push(idx[0][i][j+len1+1]);
				used[idx[0][i][j+len1+1]]=1;
			}
			else if(j+len1==len[i]-1){
				vis[idx[1][i][j-len1-1]]=c[i];
				q.push(idx[1][i][j-len1-1]);
				used[idx[1][i][j-len1-1]]=1;	
			}
			if(j==len[i]-1||s[i][j]!=s[i][j+1])
				continue;
			int len2=check2(i,j);
			if(j==len2&&j+1+len2==len[i]-1)
				ans=min(ans,c[i]);
			else if(j==len2){
				vis[idx[0][i][j+2+len2]]=c[i];
				q.push(idx[0][i][j+2+len2]);
				used[idx[0][i][j+2+len2]]=1;
			}
			else if(j+1+len2==len[i]-1){
				vis[idx[1][i][j-len2-1]]=c[i];
				q.push(idx[1][i][j-len2-1]);
				used[idx[1][i][j-len2-1]]=1;
			}
		}
	PF("%lld",spfa());
}

Measured memory_bytes: 3376