#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1000000007;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const double pi = acos(-1.0);
const double EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

template<typename T>
struct Weighted_Graph{
    struct edge{
        int to; T cost;
        edge(int to, T cost) : to(to), cost(cost) {}
    };

    vector<vector<edge>> es;
    vector<T> d;
    const T INF_T;
    const int n;

    Weighted_Graph(int n) : INF_T(numeric_limits<T>::max()), n(n){
        es.resize(n);
        d.resize(n);
    }

    void add_edge(int from, int to, T cost, bool directed = false){
        es[from].eb(to, cost);
        if(!directed) es[to].eb(from, cost);
    }

    T dijkstra(int s, int t){
        fill(all(d), INF_T);
        using P = pair<T, int>;
        priority_queue<P, vector<P>, greater<P> > que;
        que.emplace(d[s] = 0, s);
        while(!que.empty()){
            T p; int i;
            tie(p, i) = que.top(); que.pop();
            if(p > d[i]) continue;
            for(auto &e: es[i]){
                if(chmin(d[e.to], d[i]+e.cost)) que.emplace(d[e.to], e.to);
            }
        }
        return d[t];
    }
};

int main(){
    int N;
    cin >> N;
    string S[N];
    ll cost[N];
    rep(i, N){
        cin >> S[i] >> cost[i];
    }
    int s = 42*N, t = 42*N+1;
    Weighted_Graph<ll> G(42*N+2);
    rep(i, N){
        G.add_edge(s, 21*i, cost[i], true);
    }
    rep(i, N){
        rep2(j, 0, sz(S[i])){
            int ptr = 21*i+j;
            bool flag = true;
            rep2(k, j, sz(S[i])-1){
                if(S[i][k] != S[i][j+sz(S[i])-1-k]) flag = false;
            }
            if(flag) G.add_edge(ptr, t, 0, true);
            rep(k, N){
                bool flag2 = true;
                int l = j, r = sz(S[k]);
                while(l < sz(S[i]) && r > 0){
                    if(S[i][l] != S[k][r-1]) flag2 = false;
                    l++, r--;
                }
                if(flag2){
                    if(l == sz(S[i])) G.add_edge(ptr, 21*(N+k)+r, cost[k], true);
                    else G.add_edge(ptr, 21*i+l, cost[k], true);
                }
            }
        }
    }
    rep(i, N){
        rep2(j, 0, sz(S[i])){
            int ptr = 21*(N+i)+j;
            bool flag = true;
            rep2(k, 0, j-1){
                if(S[i][k] != S[i][j-1-k]) flag = false;
            }
            if(flag) G.add_edge(ptr, t, 0, true);
            rep(k, N){
                bool flag2 = true;
                int l = 0, r = j;
                while(l < sz(S[k]) && r > 0){
                    if(S[k][l] != S[i][r-1]) flag2 = false;
                    l++, r--;
                }
                if(flag2){
                    if(l == sz(S[k])) G.add_edge(ptr, 21*(N+i)+r, cost[k], true);
                    else G.add_edge(ptr, 21*k+l, cost[k], true);
                }
            }
        }
    }
    ll ans = G.dijkstra(s, t);
    cout << (ans >= INF? -1 : ans) << endl;
}

Measured memory_bytes: 3836