#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <class S, class T>
ostream& operator<<(ostream& out, const pair<S, T>& o) {
    out << "(" << o.first << "," << o.second << ")";
    return out;
}
template <class T>
ostream& operator<<(ostream& out, const vector<T>& V) {
    for (int i = 0; i < V.size(); i++) {
        out << V[i];
        if (i != V.size() - 1) out << " ";
    }
    return out;
}
template <class T>
ostream& operator<<(ostream& out, const vector<vector<T>>& Mat) {
    for (int i = 0; i < Mat.size(); i++) {
        if (i != 0) out << endl;
        out << Mat[i];
    }
    return out;
}
template <class S, class T>
ostream& operator<<(ostream& out, const map<S, T>& mp) {
    out << "{ ";
    for (auto it = mp.begin(); it != mp.end(); it++) {
        out << it->first << ":" << it->second;
        if (mp.size() - 1 != distance(mp.begin(), it)) out << ", ";
    }
    out << " }";
    return out;
}
template <typename T>
vector<T> make_v(size_t a) {
    return vector<T>(a);
}
template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T& t, const V& v) {
    t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T& t, const V& v) {
    for (auto& e : t) fill_v(e, v);
}
/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

pair<int, string> check(string L, string R) {
    size_t min_sz = min(L.length(), R.length());
    size_t L_sz = L.length();

    for (int i = 0; i < min_sz; i++) {
        if (L[L_sz - 1 - i] != R[i]) return make_pair(-1, "X");
    }
    if (L.length() < R.length()) {
        return make_pair(1, R.substr(L.length()));
    } else {
        return make_pair(0, L.substr(0, L.length() - R.length()));
    }
}
template <class Type>
Type solve(Type res = Type()) {
    int N;
    cin >> N;
    vector<string> S(N);
    vector<ll> C(N);
    for (int i = 0; i < N; i++) {
        cin >> S[i] >> C[i];
    }

    map<string, ll> memo[2];

    using Item = tuple<ll, int, string>;
    priority_queue<Item, vector<Item>, greater<Item>> pq;
    for (int i = 0; i < N; i++) {
        size_t sz = S[i].length();

        string L = "";
        for (int j = 0; j < (int)sz; j++) {
            vector<string> Rs = {S[i].substr(j), S[i].substr(j + 1)};
            for (const string& R : Rs) {
                auto ret = check(L, R);
                if (ret.first != -1) {
                    pq.emplace(C[i], ret.first, ret.second);
                    if (memo[ret.first].count(ret.second) == 0) {
                        memo[ret.first][ret.second] = LINF;
                    }
                    memo[ret.first][ret.second] =
                        min(C[i], memo[ret.first][ret.second]);
                }
            }
            L += S[i][j];
        }
    }
    // cout << memo[0] << endl;
    // cout << memo[1] << endl;

    res = LINF;
    while (pq.size()) {
        ll cost;
        int state;
        string T;
        tie(cost, state, T) = pq.top();
        pq.pop();

        if (T == "") {
            res = cost;
            break;
        }

        for (int i = 0; i < N; i++) {
            string L = (state == 0 ? T : S[i]);
            string R = (state == 0 ? S[i] : T);
            auto ret = check(L,R);
            if(ret.first == -1) continue;
            if(memo[ret.first].count(ret.second) == 0){
                memo[ret.first][ret.second] = LINF;
            }
            ll& val = memo[ret.first][ret.second];
            if(val > cost + C[i]){
                val = cost + C[i];
                pq.emplace(val,ret.first,ret.second);
            }
        }
    }
    if (res == LINF) res = -1;

    return res;
}
int main(void) {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // solve<ll>(0);
    cout << fixed << setprecision(12) << solve<ll>() << endl;
    return 0;
}

Measured memory_bytes: 3764