#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<class T,class U> inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;}
template<class T,class U> inline bool chmax(T&x,U y){if(x<y){x=y;return true;}return false;}
#define fr(i,n) for(int i=0;i<(n);++i)
#define Fr(i,n) for(int i=1;i<=(n);++i)
#define ifr(i,n) for(int i=(n)-1;i>=0;--i)
#define iFr(i,n) for(int i=(n);i>0;--i)

bool ispali(string&s){
    string t=s;
    reverse(t.begin(),t.end());
    return s==t;
}

template <class U>
inline void dkstr(vector<vector<pair<int,U>>>& v,int n,int s,vector<U>& d,U inf){
    d.assign(n,inf);
    d[s]=0;
    priority_queue<pair<U,int>,vector<pair<U,int>>,greater<pair<U,int>>> pq;
    pq.emplace(0,s);
    while(!pq.empty()){
        pair<U,int> p=pq.top();pq.pop();
        U D=p.first;
        int i=p.second;
        if(d[i]<D) continue;
        for(auto q:v[i]){
            if(d[q.first]<=D+q.second) continue;
            d[q.first]=D+q.second;
            pq.emplace(D+q.second,q.first);
        }
    }
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    vector<int> c(n);
    vector<string> s(n);
    using Type = pair<ll,pair<string,bool>>;
    priority_queue<Type,vector<Type>,greater<>> pq;
    map<pair<string,bool>,ll> mp;
    fr(i,n){
        cin>>s[i]>>c[i];
        pq.emplace(c[i],make_pair(s[i],true));
        pq.emplace(c[i],make_pair(s[i],false));
    }
    while(!pq.empty()){
        auto[D,SB]=pq.top();pq.pop();
        auto[S,B]=move(SB);
        if(ispali(S)) return cout<<D<<endl,0;
        if(B){
            fr(i,n){
                string T=s[i];
                int I{0},J=T.size()-1;
                while(I<S.size() and J>=0 and S[I]==T[J]){
                    ++I;--J;
                }
                if(I<S.size() and J>=0) continue;
                string P;
                bool b;
                if(S.size()<T.size()){
                    P=T.substr(0,T.size()-S.size());
                    b=false;
                }
                else{
                    P=S.substr(T.size());
                    b=true;
                }
                auto Pb=make_pair(P,b);
                if(mp.find(Pb)!=mp.end() and mp[Pb]<=D+c[i]) continue;
                pq.emplace(D+c[i],make_pair(P,b));
                mp[Pb]=D+c[i];
            }
        }
        else{
            fr(i,n){
                string T=s[i];
                int I=S.size()-1,J{};
                while(I>=0 and J<T.size() and S[I]==T[J]){
                    --I;++J;
                }
                if(I>=0 and J<T.size()) continue;
                string P;
                bool b;
                if(S.size()<T.size()){
                    P=T.substr(S.size());
                    b=true;
                }
                else{
                    P=S.substr(0,S.size()-T.size());
                    b=false;
                }
                auto Pb=make_pair(P,b);
                if(mp.find(Pb)!=mp.end() and mp[Pb]<=D+c[i]) continue;
                pq.emplace(D+c[i],make_pair(P,b));
                mp[Pb]=D+c[i];
            }
        }
    }
    cout<<-1<<'\n';
}

Measured memory_bytes: 3740