#include <bits/stdc++.h>
using namespace std;using ll=long long;using uint=unsigned int;using pii=pair<int,int>;using pll=pair<ll,ll>;using ull = unsigned long long;using ld=long double;template<typename T>void _(const char*s,T h){cerr<<s<<" = "<<h<<"\n";}template<typename T,typename...Ts>void _(const char*s,T h,Ts...t){int b=0;while(((b+=*s=='(')-=*s==')')!=0||*s!=',')cerr<<*s++;cerr<<" = "<<h<<",";_(s+1,t...);}// break continue pop_back 998244353
#define int ll
#define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i,n) for(int i=0;i<(n);++i)
#define sz(a)((int)(a).size())
#define sqr(x) ((x)*(x))
struct init{init(){cin.tie(0);iostream::sync_with_stdio(0);cout<<fixed<<setprecision(10);cerr<<fixed<<setprecision(5);}~init(){
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__,__VA_ARGS__)
cerr<<"Time elapsed: "<<(double)clock()/CLOCKS_PER_SEC<<"s.\n";
#else
#define dbg(...) 
#endif
}}init;template<typename T,typename U>void upx(T&x,U y){if(x<y)x=y;}template<typename T,typename U>void upn(T&x,U y){if(x>y)x=y;}mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N=50,L=20,OO=1e18,A=26,Z=N*L+2;
struct node{
    int32_t go[A],up;
    int term,pal;
    node(){}
    void clear(){
        memset(go,0,sizeof go);
        up=0;
        term=OO;
        pal=OO;
    }
}trie[Z];
int g[Z][Z];
int dist[Z];
int u[Z];

string s[N];
int cost[N];

int32_t ffr=2; // 0 is nullptr,1 is root
int32_t insert(int i){
    int32_t v=1;
    for(char c:s[i]){
        if(!trie[v].go[c-'a']){
            trie[ffr].clear();
            trie[ffr].up=v;
            trie[v].go[c-'a']=ffr++; 
        }
        v=trie[v].go[c-'a']; 
    }
    upn(trie[v].term,cost[i]);
    return v;
}
void dfs(int v,int start,int i,int j,int c){
    if(j==sz(s[i])){
        upn(g[start][v],c);
        return;
    }
    int to=trie[v].go[s[i][j]-'a'];
    if(to){
        dfs(to,start,i,j+1,c);
    }else if(v==1)return;
    if(v!=1&&trie[v].term<OO)dfs(1,start,i,j,c+trie[v].term);
}
int solve(int n){
    trie[1].clear();
    ffr=2;
    forn(i,n){
        reverse(s[i].begin(), s[i].end());
        int32_t v=insert(i),len=0;
        while(v){
            string suf=s[i].substr(sz(s[i])-len);
            string fus=suf;
            reverse(fus.begin(),fus.end());
            if(suf==fus)upn(trie[v].pal,cost[i]);
            v=trie[v].up;
            len++;
        }
        reverse(s[i].begin(), s[i].end());
    }
    for(int i=1;i<ffr;++i)
        for(int j=1;j<ffr;++j){
            if(i==j)g[i][j]=0;
            else g[i][j]=OO;
        }
    forn(i,n){
        for(int v=1;v<ffr;++v){
            dfs(v,v,i,0,cost[i]);
        }
    }
    // for(int i=1;i<ffr;++i)
    //     for(int j=1;j<ffr;++j)
    //         dbg(i,j,g[i][j]);

    for(int i=1;i<ffr;++i)
        dist[i]=OO,u[i]=0;
    dist[1]=0;
    while(1){
        int v=-1;
        for(int i=1;i<ffr;++i){
            if(!u[i]&&(v==-1||dist[v]>dist[i]))v=i;
        }
        if(v==-1||dist[v]==OO)break;
        u[v]=1;
        for(int j=1;j<ffr;++j){
            upn(dist[j],dist[v]+g[v][j]);
        }
    }
    int res=OO;
    for(int i=1;i<ffr;++i){
        // dbg(dist[i],trie[i].pal);
        upn(res,dist[i]+trie[i].pal);
    }
    // dbg(res);
    return res;
}
int32_t main(){
    int n;
    cin>>n;
    forn(i,n)cin>>s[i]>>cost[i];
    int ans=solve(n);
    forn(i,n)reverse(s[i].begin(), s[i].end());
    upn(ans,solve(n));
    if(ans==OO)ans=-1;
    cout<<ans<<'\n';
    return 0;
}

Measured memory_bytes: 11328