#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}
 
struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T1,typename T2>
ostream &operator<<(ostream &os,const pair<T1,T2>&p){
    os<<p.first<<" "<<p.second;
    return os;
}
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}

template<typename T1,typename T2>
istream &operator>>(istream &is,pair<T1,T2>&p){
    is>>p.first>>p.second;
    return is;
}

template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

template<typename T>
vector<T> dijkstra(int s,vector<vector<pair<int,T>>> &G){
    const T TINF=numeric_limits<T>::max();
    using P=pair<T,int>;
    int n=G.size();
    vector<T> d(n,TINF);
    vector<int> b(n,-1);//previous point
    priority_queue<P,vector<P>,greater<P>> q;
    d[s]=0;
    q.emplace(d[s],s);
    while(!q.empty()){
        P p=q.top();q.pop();
        int v=p.second;
        if(d[v]<p.first) continue;
        for(auto& e:G[v]){
            int u=e.first;
            T c=e.second;
            if(d[u]>d[v]+c){
                d[u]=d[v]+c;
                b[u]=v;
                q.emplace(d[u],u);
            }
        }
    }
    return d;
}

bool isPalin(const string &s){
    int n=(int)s.size();
    rep(i,n)if(s[i]!=s[n-1-i]) return false;
    return true;
}


signed main(){
    int n;cin>>n;
    vector<string> s(n);
    vector<ll> c(n);
    rep(i,n)cin>>s[i]>>c[i];

    int len=0;
    rep(i,n)chmax(len,int(s[i].size()));

    vector<vector<pair<int,ll>>> g(1);
    vector<bool> accept(1,false);
    vector<string> pref(1,""),suff(1,"");
    queue<int> que;
    que.push(0);
    while(!que.empty() and g.size()<2000000){
        int p=que.front();que.pop();
        if((int)max(pref[p].size(),suff[p].size())>len) continue;
        if(accept[p]) continue;
        if(pref[p].size()==0){
            rep(i,n){
                bool f=true;
                for(int j=0;j<min(suff[p].size(),s[i].size());j++)if(s[i][j]!=suff[p][j])f=false;
                if(f){
                    int id=g.size();
                    g[p].push_back({id,c[i]});
                    g.push_back(vector<pair<int,ll>>());
                    que.push(id);
                    accept.push_back(false);
                    if(s[i].size()<suff[p].size()){
                        string t="";
                        for(int j=s[i].size();j<suff[p].size();j++)t.push_back(suff[p][j]);
                        pref.push_back("");
                        suff.push_back(t);
                        if(isPalin(t))accept[id]=true;
                    }
                    else{
                        string t="";
                        for(int j=suff[p].size();j<s[i].size();j++)t.push_back(s[i][j]);
                        pref.push_back(t);
                        suff.push_back("");
                        if(isPalin(t))accept[id]=true;
                    }
                }
            }
        }else{
            rep(i,n){
                reverse(ALL(s[i]));
                bool f=true;
                for(int j=0;j<min(pref[p].size(),s[i].size());j++)if(s[i][j]!=pref[p][j])f=false;
                if(f){
                    int id=g.size();
                    g[p].push_back({id,c[i]});
                    g.push_back(vector<pair<int,ll>>());
                    que.push(id);
                    accept.push_back(false);
                    if(s[i].size()<pref[p].size()){
                        string t="";
                        for(int j=s[i].size();j<pref[p].size();j++)t.push_back(pref[p][j]);
                        suff.push_back("");
                        pref.push_back(t);
                        if(isPalin(t))accept[id]=true;
                    }
                    else{
                        string t="";
                        for(int j=pref[p].size();j<s[i].size();j++)t.push_back(s[i][j]);
                        suff.push_back(t);
                        pref.push_back("");
                        if(isPalin(t))accept[id]=true;
                    }
                }
                reverse(ALL(s[i]));
            }
        }
    }
    // rep(i,g.size()){
    //     cout<<"node "<<i<<endl;
    //     cout<<"pref : "<<pref[i]<<endl;
    //     cout<<"suff : "<<suff[i]<<endl;
    //     cout<<"AC ? "<<(accept[i]?"O":"X")<<endl;
    //     for(auto p:g[i]){
    //         cout<<"to "<<p.first<<" cost "<<p.second<<endl;
    //     }
    //     cout<<endl;
    // }
    auto d=dijkstra(0,g);
    ll ans=LINF;
    rep(i,d.size())if(accept[i])chmin(ans,d[i]);
    cout<<(ans<LINF?ans:-1)<<endl;
    return 0;
}


Measured memory_bytes: 269676