#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;

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

ll fnc(ll b,ll p,ll g){
	if(p == s[b].size()) 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) {
		for(int j=p, k=s[b].size()-1; j<k; j++, k--) {
			if(s[b][j] != s[b][k]) l = 0;
		}
	}
	else {
		for(int j=0, k=p; j<k; j++, k--) {
			if(s[b][j] != s[b][k]) l = 0;
		}
	}
	if(l) return m = 0;
	for(int i=0; i<n; i++) {
		ll y = 1;
		int j, k;
		if(!g) {
			for(j=p, k=s[i].size()-1; j<s[b].size() && k>=0 && y; j++, k--) {
				if(s[b][j] != s[i][k]) y = 0;
			}
			if(y) {
				if(k < 0) m = min(m, c[i] + fnc(b, j, 0));
				else m = min(m, c[i] + fnc(i, k, 1));
			}
		}
		else {
			for(j=p, k=0; k<s[i].size() && j>=0 && y; j--, k++) {
				if(s[b][j] != s[i][k]) y = 0;
			}
			if(y) {
				if(j < 0) m = min(m, c[i] + fnc(i, k, 0));
				else m = min(m, c[i] + fnc(b, j, 1));
			}
		}
	}
	return m;
}

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