#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(9), 0);
//int SEGV = getenv("D") || (exit(system("D= SEGFAULT_SIGNALS=all catchsegv ./prog.exe") >> 8), 0);
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
Int dp[50][2][30];
const Int INF = 1e18;
int main() {
    int N; cin >> N;
    vector<string> S(N);
    vector<int> C(N);
    for (int i = 0; i < N; i++) cin >> S[i] >> C[i];
    MinHeap<tuple<Int, int, int, int>> Q;
    for (int i = 0; i < N; i++) {
        for (int b = 0; b < 2; b++) {
            for (int n = 0; n <= 20; n++) {
                dp[i][b][n] = INF;
            }
        }
    }
    auto enq = [&](Int cost, int i, int b, int n) {
        if (n == 0) i = 0, b = 0;
        if (cost < dp[i][b][n]) {
            Q.emplace(cost, i, b, n);
            dp[i][b][n] = cost;
        }
    };
    for (int i = 0; i < N; i++) {
        // a|a
        for (int center = 0; center <= S[i].size(); center++) {
            bool eql = true;
            int sz = min(center, (int)S[i].size() - center);
            for (int j = 0; j < sz; j++) {
                if (S[i][center-1-j] != S[i][center+j]) eql = false;
            }
            if (eql) {
                enq(C[i], i, (center <= S[i].size() - center), S[i].size() - 2*sz);
            }
        }
        // a[b]a
        for (int center = 0; center < S[i].size(); center++) {
            bool eql = true;
            int sz = min(center, (int)S[i].size() - center - 1);
            for (int j = 0; j < sz; j++) {
                if (S[i][center-1-j] != S[i][center+1+j]) eql = false;
            }
            if (eql) {
                enq(C[i], i, (center <= S[i].size() - center - 1), S[i].size() - 1 - 2*sz);
            }
        }
    }
    while (!Q.empty()) {
        auto [cost, i, b, n] = Q.top();
        Q.pop();
        if (cost != dp[i][b][n]) continue;
        if (n == 0) {
            cout << cost << endl;
            return 0;
        }
        if (b == 0) {
            for (int j = 0; j < N; j++) {
                bool eql = true;
                int sz = min(n, (int)S[j].size());
                for (int k = 0; k < sz; k++) {
                    if (S[i][n - 1 - k] != S[j][k]) eql = false;
                }
                if (eql) {
                    if (n <= S[j].size()) {
                        enq(cost + C[j], j, 1, S[j].size() - n);
                    } else {
                        enq(cost + C[j], i, 0, n - S[j].size());
                    }
                }
            }
        } else {
            for (int j = 0; j < N; j++) {
                bool eql = true;
                int sz = min(n, (int)S[j].size());
                for (int k = 0; k < sz; k++) {
                    if (S[j][S[j].size() - 1 - k] != S[i][S[i].size() - n + k]) eql = false;
                }
                if (eql) {
                    if (n <= S[j].size()) {
                        enq(cost + C[j], j, 0, S[j].size() - n);
                    } else {
                        enq(cost + C[j], i, 1, n - S[j].size());
                    }
                }
            }
        }
    }
    cout << -1 << endl;
    return 0;
}


Measured memory_bytes: 3704