#include<bits/stdc++.h>
#define maxn 55
#define pli pair<long long,int>
using namespace std;
string s[maxn<<1];
bool cmp(int idx1,int st1,int idx2,int st2,int L) {
	for(int i=0;i<L;i++)
		if(s[idx1][st1 + i] != s[idx2][st2 + i])
			return false;
	return true;
}
int cost[maxn<<1];
struct edge {
	int v,cost;
};
vector<edge> G[maxn*2*21 + 5];
void build_graph(int N) {
	for(int i=1;i<=2*N;i++) {
		int a = (i <= N ? N + 1 : 1) , b = (a == 1 ? N : 2 * N);
		for(int j=a;j<=b;j++) {
			int sz1 = (int)s[i].size() , sz2 = (int)s[j].size();
			for(int k=0;k<sz1;k++) {
				int L = min(sz1 - k, sz2);
				if(cmp(i,k,j,0,L)) {
					int u = (i - 1) * 21 + k , v;
					if(L == sz2)
						v = (i - 1) * 21 + k + L;
					else
						v = (j - 1) * 21 + L;
					G[u].push_back(edge{v,cost[j]});
				}
			}
		}
	}
	int st = N * 2 * 21 + 1;
	for(int i=1;i<=2*N;i++) {
		int v = (i - 1) * 21 + 0;
		G[st].push_back(edge{v,cost[i]});
	}
}
long long dis[maxn*2*21 + 5];
void dijkstra(int N) {
	int st = N * 2 * 21 + 1;
	
	memset(dis,-1,sizeof dis);
	dis[st] = 0;
	priority_queue<pli> pq;
	pq.push({-dis[st],st});
	
	while(!pq.empty()) {
		pli now = pq.top();
		pq.pop();
		int u = now.second;
		if(-now.first != dis[u])	continue;
		for(edge e : G[u]) {
			int v = e.v , cost = e.cost;
			if(dis[v] == -1 || dis[u] + cost < dis[v]) {
				dis[v] = dis[u] + cost;
				pq.push({-dis[v],v});
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int N;
	cin >> N;
	for(int i=1;i<=N;i++) {
		cin >> s[i] >> cost[i];
		s[i + N] = s[i];
		
		reverse(s[i + N].begin(), s[i + N].end());
		cost[i + N] = cost[i];
	}
	
	build_graph(N);
	dijkstra(N);
	
	long long ans = -1;
	for(int i=1;i<=2*N;i++) {
		int state = (i - 1) * 21;
		for(int j=0;j<(int)s[i].size();j++,state++) {
			if(dis[state] == -1)	continue;
			int l = j , r = (int)s[i].size() - 1;
			bool flag = true;
			for(;l<=r;l++,r--)
				if(s[i][l] != s[i][r]) {
					flag = false;
					break;
				}
			if(flag && (ans == -1 || dis[state] < ans))
				ans = dis[state];
		}
		if(dis[state] != -1 && (ans == -1 || dis[state] < ans))
			ans = dis[state];
	}
	
	cout << ans << endl;
	return 0;
}