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

typedef long long ll;
typedef pair<ll,ll> pi;
typedef vector <ll> vi;
typedef vector <pi> vpi;
#define f first
#define s second
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define aFOR(i,x) for (auto i: x)
#define mem(x,i) memset(x,i,sizeof x)
#define fast ios_base::sync_with_stdio(false),cin.tie(0)
#define maxn 51
#define INF (ll)1e18
#define int ll
int N;
string S[maxn][2];
int C[maxn];
map <string, int> dp[2];

bool ispalin(string str){
	string str2 = str; reverse(all(str2));
	return (str == str2);
}
int dpf(string str,int j){
	if (ispalin(str)) return 0;
	
	if (dp[j].find(str) != dp[j].end()) return dp[j][str];
	dp[j][str] = INF;

	
	FOR(i,1,N){
		bool ok = 1;
		int f = (j+1) % 2;
		FOR(k,0,min(S[i][f].size(), str.size()) - 1) if (S[i][f][k] != str[k]) ok = 0;
		if (!ok) continue;
		if (S[i][f].size() < str.size()){
			string nxt = "";
			FOR(k,S[i][f].size(), str.size() - 1) nxt += str[k];
			dp[j][str] = min(dp[j][str], dpf(nxt, j) + C[i]);
		}else{
			string nxt = "";
			FOR(k,str.size(), S[i][f].size() - 1) nxt += S[i][f][k];
			dp[j][str] = min(dp[j][str], dpf(nxt, f) + C[i]);
		}
	}
		
	return dp[j][str];
}
int32_t main(){
	fast;
	
	cin>>N;
	FOR(i,1,N){
		cin>>S[i][0]>>C[i];
		S[i][1] = S[i][0];
		reverse(all(S[i][1]));
	}
	int ans = INF;
	FOR(i,1,N){
		ans = min(ans, dpf(S[i][0],0) + C[i]);
	}
	
	cout<<(ans == INF ? -1 : ans);
}

