#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}

typedef tuple<LL,int,int,int> T;
LL dp[55][2][25];
vector<T> path[55][2][25];
priority_queue<T,vector<T>,greater<T>> q1;

int main(){
    int n,m;
    int i,j,k;
    LL a,b,c,d;
    LL x,y,z,p;
    cin>>n;
    vector<LL> val;
    vector<string> vs;
    string sa;
    for(i=0;i<n;i++){
        cin>>sa>>a;
        vs.push_back(sa);
        val.push_back(a);
    }
    memset(dp,-1,sizeof(dp));
    for(i=0;i<n;i++){
        for(j=0;j<vs[i].size();j++){
            for(k=0;k<n;k++){
                for(a=j,b=vs[k].size()-1;a<vs[i].size() && b>=0;a++,b--){
                    if(vs[i][a]!=vs[k][b])break;
                }
                if(a==vs[i].size() && b==-1){
                    path[i][0][j].push_back(make_tuple(-1,0,0,val[k]));
                }else if(a==vs[i].size()){
                    path[i][0][j].push_back(make_tuple(k,1,b,val[k]));
                }else if(b==-1){
                    path[i][0][j].push_back(make_tuple(i,0,a,val[k]));
                }
                
                for(a=j,b=0;a>=0 && b<vs[k].size();a--,b++){
                    if(vs[i][a]!=vs[k][b])break;
                }
                if(a==-1 && b==vs[k].size()){
                    path[i][1][j].push_back(make_tuple(-1,0,0,val[k]));
                }else if(a==-1){
                    path[i][1][j].push_back(make_tuple(k,0,b,val[k]));
                }else if(b==vs[k].size()){
                    path[i][1][j].push_back(make_tuple(i,1,a,val[k]));
                }
            }
            a=(vs[i].size()-j)/2;
            for(k=0;k<a;k++){
                if(vs[i][j+k]!=vs[i][vs[i].size()-k-1])break;
            }
            if(k==a)path[i][0][j].push_back(make_tuple(-1,0,0,0));
            a=(j+1)/2;
            for(k=0;k<a;k++){
                if(vs[i][j-k]!=vs[i][k])break;
            }
            if(k==a)path[i][1][j].push_back(make_tuple(-1,0,0,0));
        }
    }
    for(i=0;i<n;i++){
        q1.push(make_tuple(val[i],i,0,0));
    }
    
    while(!q1.empty()){
        tie(d,a,b,c)=q1.top(),q1.pop();
        if(a==-1){
            cout<<d<<endl;
            return 0;
        }
        if(dp[a][b][c]!=-1)continue;
        dp[a][b][c]=d;
        for(auto to:path[a][b][c]){
            tie(x,y,z,p)=to;
            if(x==-1 || dp[x][y][z]==-1)q1.push(make_tuple(d+p,x,y,z));
        }
    }
    cout<<-1<<endl;
}

Measured memory_bytes: 3836