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

#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int maxn = 60;
const ll oo = 1ll<<60;
ll memo[maxn][maxn][3],C[maxn];
string str[maxn];
int N;

//0 derecha
//1 izquierda
string get(string &tmp,int pos,int dir){
	string res = "";
	if(dir == 0){
		forr(i,(int)tmp.size()-1,pos)res += tmp[i];
	}else{
		forr(i,pos,0)res += tmp[i];
	}
	return res;
}
int sufijo(string &tmp,string &necesito){
	int ini = necesito.size()-1,fin = tmp.size()-1;
	int res = 0;
	int i = 0;
	while(i < (int)min(tmp.size(),necesito.size())){
		if(necesito[ini] != tmp[fin])break;
		ini--; fin--;
		res++;
	}
	return res;
}

int prefijo(string &tmp,string &necesito){
	int ini = 0,fin = 0;
	int res = 0;
	int i = 0;
	while(i < (int)min(tmp.size(),necesito.size())){
		if(necesito[ini] != tmp[fin])break;
		ini++; fin++;
		res++;
	}
	return res;
}

bool palindromo(string a){
	string aa = a;
	reverse(all(aa));
	return a == aa;
}

ll dp(int pos1,int pos2,int dir){
	if(pos2 >= (int)str[pos1].size() || pos2 < 0)return 0;
	ll &res = memo[pos1][pos2][dir];
	if(res != -1)return res;
	res = oo;
	string necesito = get(str[pos1],pos2,dir);
	if(palindromo(necesito)){
      res = 0;
      return res;
    }
	forn(i,0,N){
		if(dir == 0){
			int cnt = sufijo(str[i],necesito);
			if(!cnt)continue;
			if(cnt < (int)necesito.size() && cnt < (int)str[i].size())continue;
			if(cnt < (int)necesito.size()){
				res = min(res,C[i]+dp(pos1,pos2+cnt,dir));
			}else{
				res = min(res,C[i]+dp(i,(int)str[i].size()-1-cnt,!dir));
			}
		}else{
			int cnt = prefijo(str[i],necesito);
			if(!cnt)continue;
			if(cnt < (int)necesito.size() && cnt < (int)str[i].size())continue;
			if(cnt < (int)necesito.size()){
				res = min(res,C[i]+dp(pos1,pos2-cnt,dir));
			}else{
				res = min(res,C[i]+dp(i,cnt,!dir));
			}
		}
	}
	return res;
}
#define no cout << "-1\n";
int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	forn(i,0,maxn)forn(j,0,maxn)forn(k,0,3)memo[i][j][k] = -1;
	cin >> N;
	forn(i,0,N)cin >> str[i] >> C[i];
	ll res = oo;
	forn(i,0,N)if(palindromo(str[i]))res = min(res,ll(C[i]));
	forn(i,0,N)res = min(res,C[i]+dp(i,0,0));
	if(res >= oo){
		no;
		return 0;
	}
	cout << res << '\n';
	return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/
