#include <cassert>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;

bool rev(string &s, int i, int j, string &t, int k, int l) {
    assert(j - i == l - k);
    assert(j <= s.size());
    assert(l <= t.size());
    while (i < j) {
        if (s[i] != t[l-1]) return false;
        i++; l--;
    }
    return true;
}

bool palindrome(string &s, int i, int j) {
    assert (i <= j);
    assert (j <= s.size());
    return rev(s, i, j, s, i, j);
}

int main() {
    int N; cin >> N;
    string s[N];
    int C[N];
    for (int i = 0; i < N; i++) cin >> s[i] >> C[i];
    int pref[N][21], suf[N][21], idx = 0;
    const int goal = idx++;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j <= s[i].size(); j++) {
            pref[i][j] = idx++;
            suf[i][j] = idx++;
        }
    }
    vector<pair<int, int> > adj[idx];
    for (int i = 0; i < N; i++) {
        for (int j = 0; j <= s[i].size(); j++) {
            for (int k = 0; k < N; k++) {
                const int ni = s[i].size(), nk = s[k].size();
                if (j > nk) {
                    if (rev(s[i], j-nk, j, s[k], 0, nk)) {
                        adj[pref[i][j]].push_back({pref[i][j-nk], C[k]});
                    }
                    if (rev(s[i], ni-j, ni-j+nk, s[k], 0, nk)) {
                        adj[suf[i][j]].push_back({suf[i][j-nk], C[k]});
                    }
                } else {
                    if (rev(s[i], 0, j, s[k], 0, j)) {
                        adj[pref[i][j]].push_back({suf[k][nk-j], C[k]});
                    }
                    if (rev(s[i], ni-j, ni, s[k], nk-j, nk)) {
                        adj[suf[i][j]].push_back({pref[k][nk-j], C[k]});
                    }
                }
            }
            if (palindrome(s[i], 0, j))
                adj[pref[i][j]].push_back({goal, 0});
            if (palindrome(s[i], s[i].size()-j, s[i].size()))
                adj[suf[i][j]].push_back({goal, 0});
        }
    }

    long long dist[idx];
    const long long INF = 1LL<<60;
    fill(dist, dist+idx, INF);
    priority_queue<pair<long long, int> > pq;
    for (int i = 0; i < N; i++) {
        int u = pref[i][s[i].size()], v = suf[i][s[i].size()];
        dist[u] = dist[v] = C[i];
        pq.push({-C[i], u});
        pq.push({-C[i], v});
    }
    while (!pq.empty()) {
        auto [d, u] = pq.top();
        d = -d;
        pq.pop();
        if (d > dist[u]) continue;
        for (auto [v, e] : adj[u]) {
            if (d + e < dist[v]) {
                dist[v] = d + e;
                pq.push({-d-e, v});
            }
        }
    }
    // for (int i = 0; i < idx; i++) cerr << i << " " << dist[i] << endl;
    // for (int i = 0; i < idx; i++) {
    //     for (auto [u, d] : adj[i])
    //         cerr << i << " " << u << " " << d << endl;
    // }
    cout << (dist[goal] == INF ? -1 : dist[goal]) << endl;
}
