#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
#define yn {puts("Yes");}else{puts("No");}
struct edge{ ll to, cost; };
const ll INF = 1001001001001001001;
const int MAX_N = 200005;
const int MAX_V = 200005;
vector<edge> G[MAX_V];
ll d[MAX_V];

void dijkstra(int s){
    // greater<P>を指定することでfirstが小さい順に取り出せるようにする
    priority_queue<P, vector<P>, greater<P> > que;
    fill(d, d+MAX_V, INF);
    d[s] = 0;
    que.push(P(0, s));

    while(!que.empty()){
        P p = que.top();
        que.pop();
        int v = p.second;
        for(int i=0; i<G[v].size(); i++){
            edge e = G[v][i];
            if(d[e.to] > d[v] + e.cost){
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
}

int main() {
    int n;
    cin >> n;
    string s[n][2];
    ll c[n];
    rep(i,n) cin >> s[i][0] >> c[i];
    rep(i,n){
        s[i][1] = s[i][0];
        reverse(s[i][1].begin(), s[i][1].end());
    }
    int m[n];
    set<string> se[2];
    rep(i,n){
        m[i] = s[i][0].size();
        rep(j,2){
            string tmp;
            rep(k,m[i]){
                tmp = s[i][j].substr(k);
                se[j].insert(tmp);
            }
        }
    }

    map<string,int> mp[2];
    int cnt = 1;
    rep(j,2){
        for(auto  x : se[j]){
            mp[j][x] = cnt;
            cnt++;
        }
    }


    rep(j,2){
        rep(i,n){
            edge e;
            e.to = mp[j][s[i][j]];
            e.cost = c[i];
            G[0].push_back(e);
        }
    }

    rep(k,2){
        for(auto p : mp[k]){
            int id = p.second;
            string ss = p.first;
            int mm = ss.size();
            rep(i,n){
                if(m[i] == mm){
                    if(ss == s[i][(k+1)%2]){
                        edge e;
                        e.to = cnt;
                        e.cost = c[i];
                        G[id].push_back(e);
                    }
                }else if(m[i] < mm){
                    int ng = 0;
                    rep(j,m[i]){
                        if(ss[j] != s[i][(k+1)%2][j]){
                            ng = 1;
                            break;
                        }
                    }
                    if(ng == 0){
                        string tmp = ss.substr(m[i]);
                        int id2 = mp[k][tmp];
                        edge e;
                        e.to = id2;
                        e.cost = c[i];
                        G[id].push_back(e);
                    }
                }else{
                    int ng = 0;
                    rep(j,mm){
                        if(ss[j] != s[i][(k+1)%2][j]){
                            ng = 1;
                            break;
                        }
                    }
                    if(ng == 0){
                        string tmp = s[i][(k+1)%2].substr(mm);
                        int id2 = mp[(k+1)%2][tmp];
                        edge e;
                        e.to = id2;
                        e.cost = c[i];
                        G[id].push_back(e);
                    }
                }
            }
        }
    }

    dijkstra(0);


    ll ans = d[cnt];
    rep(i,n){
        if(s[i][0] == s[i][1]){
            ans = min(ans, c[i]);
        }
    }

    rep(j,2){
        for(auto p : mp[j]){
            string ss = p.first;
            string tt = ss;
            reverse(tt.begin(), tt.end());
            if(ss == tt){
                int id = p.second;
                ans = min(ans, d[id]);
            }
        }
    }


    if(ans >= INF) ans = -1;
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 10200