#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main(){
    int N;
    cin >> N;
    vector<string> S(N);
    vector<ll> C(N);
    for (int i = 0; i < N; i++) {
        cin >> S[i] >> C[i];
    }
    
    vector<vector<pair<ll, ll>>> edge(N*40+1);
    for (int i = 0; i < N; i++) {
        int K = S[i].size();
        for (int j = 0; j < K; j++) {
            string ss = S[i].substr(K-j-1, j+1);
            ll K1 = j+1;
            for (int k = 0; k < N; k++) {
                ll K2 = S[k].size();
                string R = S[k];
                reverse(R.begin(), R.end());
                if (K1 < K2) {
                    if (R.substr(0, K1) == ss) {
                        edge[i*20+j].push_back({(k+N)*20+K2-K1-1, C[k]});
                    }
                }
                if (K1 > K2) {
                    if (ss.substr(0, K2) == R) {
                        edge[i*20+j].push_back({i*20+j-K2, C[k]});
                    }
                }
                string T = ss+S[k], U = T;
                reverse(U.begin(), U.end());
                if (T == U) {
                    edge[i*20+j].push_back({N*40, C[k]});
                }
            }
        }
    }
    
    for (int i = 0; i < N; i++) {
        int K = S[i].size();
        for (int j = 0; j < K; j++) {
            string ss = S[i].substr(0, j+1);
            ll K2 = j+1;
            for (int k = 0; k < N; k++) {
                ll K1 = S[k].size();
                string R = S[k];
                reverse(R.begin(), R.end());
                if (K1 < K2) {
                    if (R == ss.substr(K2-K1, K1)) {
                        edge[(i+N)*20+j].push_back({(i+N)*20+j-K1, C[k]});
                    }
                }
                if (K1 > K2) {
                    if (R.substr(K1-K2, K2) == ss) {
                        edge[(i+N)*20+j].push_back({k*20+K1-K2-1, C[k]});
                    }
                }
                string T = S[k]+ss, U = T;
                reverse(U.begin(), U.end());
                if (T == U) {
                    edge[(i+N)*20+j].push_back({N*40, C[k]});
                }
            }
        }
    }
    
    ll inf = 1e18;
    vector<ll> dist(N*40+1, inf);
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
    for (int i = 0; i < N; i++) {
        int K = S[i].size();
        dist[i*20+K-1] = C[i];
        pq.push({C[i], i*20+K-1});
        string T = S[i];
        reverse(T.begin(), T.end());
        if (S[i] == T) dist[N*40] = min(dist[N*40], C[i]);
    }
    while (!pq.empty()) {
        int now = pq.top().second;
        ll d = pq.top().first;
        pq.pop();
        if (d > dist[now]) continue;
        for (auto p : edge[now]) {
            int nxt = p.first;
            ll nd = d+p.second;
            if (dist[nxt] <= nd) continue;
            dist[nxt] = nd;
            pq.push({nd, nxt});
        }
    }
    
    if (dist[N*40] == inf) cout << -1 << endl;
    else cout << dist[N*40] << endl;
	return 0;
}
