#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pis = pair<int,string>;
#ifdef LOCAL
#define dumpif(c,x) if ( c ) {cerr << "line " << __LINE__ << " : " << #x << " = " << (x) << "\n";}
#else
#define dumpif(c,x) 0
#endif

struct Edge {
    int to;
    ll cost;
    Edge(int t, ll c) : to(t), cost(c) {}
};

vector<ll> Dijkstra(const vector<vector<Edge>>& g, int s) {
    ll INF = 1LL<<60;
    vector<ll> d(g.size(), INF);
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> q;
    d.at(s) = 0;
    q.emplace(d.at(s), s);
    while ( !q.empty() ) {
        auto p = q.top();
        q.pop();
        int v = p.second;
        if ( d.at(v) < p.first ) continue;
        for ( auto e : g[v]) {
            if ( d.at(e.to) > d.at(v) + e.cost ) {
                d.at(e.to) = d.at(v) + e.cost;
                q.emplace(d.at(e.to), e.to);
            }
        }
    }
    return d;
}

ll solve() {
    ll N;
    cin >> N;
    vector<string> S(N);
    vector<ll> C(N);
    for ( int i = 0; i < N; i++ ) {
        cin >> S[i] >> C[i];
    }
    vector<string> rS(S);
    for ( int i = 0; i < N; i++ ) {
        reverse(rS[i].begin(), rS[i].end());
    }
    vector<map<string,int>> dic(2);
    vector<pis> rdic;
    int ix = 0;
    dic[0][""] = ix++;  // st
    rdic.push_back(pis(0,""));
    dic[1][""] = ix++;  // ed
    rdic.push_back(pis(1,""));
    for ( int i = 0; i < N; i++ ) {
        for ( int j = 0; j < S[i].size(); j++ ) {
            string t = S[i].substr(j);
            if ( dic[0].count(t) ==  0) {
                dic[0][t] = ix++;
                rdic.push_back(pis(0,t));
            }
        }
    }
    for ( int i = 0; i < N; i++ ) {
        for ( int j = 0; j < rS[i].size(); j++ ) {
            string t = rS[i].substr(j);
            if ( dic[1].count(t) ==  0) {
                dic[1][t] = ix++;
                rdic.push_back(pis(1,t));
            }
        }
    }
    
    int m = rdic.size();
    vector<vector<Edge>> G(m);
    for ( int i = 0; i < N; i++ ) {
        int to = dic[0][S[i]];
        G[0].push_back(Edge(to, C[i]));
    }
    for ( int i = 2; i < rdic.size(); i++ ) {
        string t1 = rdic[i].second, t2 = t1;
        reverse(t2.begin(), t2.end());
        if ( t1 == t2 ) {
            G[i].push_back(Edge(1,0LL));
        }
    }
    for ( int i = 0; i < N; i++ ) {
        for ( int j = 0; j < S[i].size(); j++ ) {
            string s1 = S[i].substr(j); // state
            for ( int k = 0; k < N; k++ ) {
                if ( rS[k].size() < s1.size() ) {
                    // dumpif(true, s1 + " " + rS[k]);
                    if ( s1.substr(0,rS[k].size()) == rS[k] ) {
                        string s2 = s1.substr(rS[k].size());
                        int i1 = dic[0][s1], i2 = dic[0][s2];
                        // dumpif(true, to_string(i1) + ":" + s1 + " " + to_string(i2) + ":" + s2);
                        G[i1].push_back(Edge(i2,C[k]));
                    }
                } else {
                    // dumpif(true, s1 + " " + rS[k]);
                    if ( s1 == rS[k].substr(0,s1.size()) ) {
                        string s2 = rS[k].substr(s1.size());
                        int i1 = dic[0][s1], i2 = dic[1][s2];
                        // dumpif(true, to_string(i1) + ":" + s1 + " " + to_string(i2) + ":" + s2);
                        G[i1].push_back(Edge(i2,C[k]));
                    }
                }
            }
        }
    }
    for ( int i = 0; i < N; i++ ) {
        for ( int j = 0; j < rS[i].size(); j++ ) {
            string s1 = rS[i].substr(j); // state
            for ( int k = 0; k < N; k++ ) {
                if ( S[k].size() <= s1.size() ) {
                    // dumpif(true, s1 + " " + S[k]);
                    if ( s1.substr(0,S[k].size()) == S[k] ) {
                        string s2 = s1.substr(S[k].size());
                        int i1 = dic[1][s1], i2 = dic[1][s2];
                        // dumpif(true, to_string(i1) + ":" + s1 + " " + to_string(i2) + ":" + s2);
                        G[i1].push_back(Edge(i2,C[k]));
                    }
                } else {
                    // dumpif(true, s1 + " " + S[k]);
                    if ( s1 == S[k].substr(0,s1.size()) ) {
                        string s2 = S[k].substr(s1.size());
                        int i1 = dic[1][s1], i2 = dic[0][s2];
                        // dumpif(true, to_string(i1) + ":" + s1 + " " + to_string(i2) + ":" + s2);
                        G[i1].push_back(Edge(i2,C[k]));
                    }
                }
            }
        }
    }    

    ll INF = 1LL<<60;
    auto dist = Dijkstra(G, 0);
    ll ans = dist[1] >= INF ? -1 : dist[1];
    return ans;
}

int main() {
    auto ans = solve();
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 3972