#include <iostream>
#include <queue>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll , int> pli;
ll dis[10101]; bool vis[10101];
vector <pli > o[10101];
priority_queue <pli , vector<pli> , greater<pli> > Q;
int n;
char s[55][23];
int len[55] , c[55];
int S = 0 , T = 0;

void dij(){
	for(int i = 1 ; i <= T ; ++ i)
		dis[i] = -1;
	dis[S] = 0; Q.push(make_pair(0 , S));
	while(!Q.empty()){
		ll d = Q.top().first; 
		int now = Q.top().second; Q.pop();
		if(vis[now]) continue;
		vis[now] = 1;
		for(int i = 0 ; i < o[now].size() ; ++ i){
			int to = o[now][i].second; ll nl = d + o[now][i].first;
			if(dis[to] == -1 || nl < dis[to]){
				dis[to] = nl;
				Q.push(make_pair(dis[to] , to));
			}
		}
	}
}

inline int Point(int x , int y , int z){
	return (x - 1) * 42 + 21 * z + y + 1;
}
void add(int s , int t , int v){
//	cout << s << " " << t << " " << v << endl;
	o[s].push_back(make_pair(v , t));
}
int main(){
	scanf("%d" , &n);
	for(int i = 1 ; i <= n ; ++ i){
		scanf("%s%d" , s[i] + 1 , &c[i]);
		len[i] = strlen(s[i] + 1);
	}
	T = n * 42 + 1;
//	cout << "Start : " << S << " End : " << T << endl; 
	for(int i = 1 ; i <= n ; ++ i)
		for(int j = 1 ; j <= n ; ++ j)
			if(i != j){
			//	cout << i << " " << j << endl;
				for(int k = 0 ; k < len[i] ; ++ k){
					int r = len[j] + 1 , t = k;
					while(r > 1 && t < len[i] && s[i][t + 1] == s[j][r - 1]){
						t ++;
						r --;
					}
					if(r == 1 && t == len[i])
						add(Point(i , k , 0) , Point(i , t , 0) , c[i] + c[j]);
					else if(r == 1)
						add(Point(i , k , 0) , Point(i , t , 0) , c[j]);
					else if(t == len[i])
						add(Point(i , k , 0) , Point(j , len[j] + 1 - r , 1) , c[i]);
				}
			}
			else{
				for(int k = 0 ; k < len[i] ; ++ k){
					int r = len[i] + 1 , t = k;
					while(r > 1 && t < len[i] && t <= r && s[i][t + 1] == s[i][r - 1]){
						t ++;
						r --;
					}
					if(r - t <= 1)
						add(Point(i , k , 0) , T , c[i]);
				}
			}
	for(int i = 1 ; i <= n ; ++ i)
		for(int j = 1 ; j <= n ; ++ j)
			if(i != j){
				for(int k = len[i] + 1 ; k > 1 ; -- k){
					int r = 0 , t = k;
					while(r < len[j] && t > 1 && s[i][t - 1] == s[j][r + 1]){
						t --;
						r ++;
					}
					if(r == len[j] && t == 1)
						add(Point(i , len[i] + 1 - k , 1) , Point(i , len[i] + 1 - t , 1) , c[i] + c[j]);
					else if(r == len[j])
						add(Point(i , len[i] + 1 - k , 1) , Point(i , len[i] + 1 - t , 1) , c[j]);
					else if(t == 1)
						add(Point(i , len[i] + 1 - k , 1) , Point(j , r , 0) , c[i]);
				}
			}
			else{
				for(int k = len[i] + 1 ; k > 1 ; -- k){
					int r = 0 , t = k;
					while(r < len[i] && t > 1 && r <= t && s[i][r + 1] == s[i][t - 1]){
						t --;
						r ++;
					}
					if(t - r <= 1){
				//		cout << i << " " << k << endl;
						add(Point(i , len[i] + 1 - k , 1) , T , c[i]);
					}
				}
			}
	for(int i = 1 ; i <= n ; ++ i)
		for(int j = 0 ; j <= 1 ; ++ j){
			add(S , Point(i , 0 , j) , 0); add(Point(i , len[i] , j) , T , 0);
		}
	dij();
	printf("%lld\n" , dis[T]);
//	cout << dis[43] << " " << dis[45] << endl;
//	for(int i = 1 ; i <= T ; ++ i)
//		if(dis[i] == 0)
//			cout << i << " ";
//	cout << endl;
	return 0;
} 

Measured memory_bytes: 4108