#include <utility>

#include "bits/stdc++.h"
using namespace std;
typedef long long li;

int is_palindrome(const string& s) {
    li p, q;
    if (s.size() % 2 == 0) {
        p = s.size() / 2 - 1;
        q = p + 1;
    } else {
        p = s.size() / 2 - 1;
        q = p + 2;
    }
    while (p >= 0) {
        if (s[p] != s[q]) {
            return false;
        }
        p--;
        q++;
    }
    return true;

}

string rev(string s) {
    string ret = std::move(s);
    reverse(ret.begin(), ret.end());
    return ret;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    li n;
    cin >> n;
    vector<string> ss(n), rev_ss(n);
    vector<li> cs(n);
    for (int i = 0; i < n; ++i) {
        cin >> ss[i] >> cs[i];
        rev_ss[i] = ss[i];
        reverse(rev_ss[i].begin(), rev_ss[i].end());
    }

    priority_queue<pair<li, pair<string, bool>>> q;
    map<pair<string, bool>, li> memo;
    for (int i = 0; i < n; ++i) {
        for (int preflen = 0; preflen <= ss[i].size(); ++preflen) {
            if (is_palindrome(ss[i].substr(preflen))) {
                pair<string, bool> state = make_pair(ss[i].substr(0, preflen), true);
                if (memo.find(state) == memo.end() || memo[state] > cs[i]) {
                    q.emplace(-cs[i], state);
                    memo[state] = cs[i];
                }
            }
        }

        for (int suflen = 0; suflen <= ss[i].size(); ++suflen) {
            if (is_palindrome(rev_ss[i].substr(suflen))) {
                pair<string, bool> state = make_pair(rev_ss[i].substr(0, suflen), false);
                if (memo.find(state) == memo.end() || memo[state] > cs[i]) {
                    q.emplace(-cs[i], state);
                    memo[state] = cs[i];
                }
            }
        }
    }

    while (not q.empty()) {
        auto top = q.top();
        q.pop();
        const li cost = -top.first;
        const string extra = top.second.first;
        const bool is_pref = top.second.second;

//        cout << cost << " " << extra << " " << is_pref << endl;
//        continue;

        if (extra.empty()) {
            cout << cost << endl;
            return 0;
        }

        string rev_extra = extra;
        reverse(rev_extra.begin(), rev_extra.end());

        if (is_pref) {
            for (int i = 0; i < n; ++i) {
                if (ss[i].substr(0, rev_extra.size()) == rev_extra) {
                    const li ncost = cost + cs[i];

                    const pair<string, bool> nstate = make_pair(rev(ss[i].substr(rev_extra.size())), false);
                    if (memo.find(nstate) == memo.end() || memo[nstate] > ncost) {
                        q.emplace(-ncost, nstate);
                        memo[nstate] = ncost;
                    }
                } else if (ss[i].size() < rev_extra.size() && ss[i] == rev_extra.substr(0, ss[i].size())) {
                    const li ncost = cost + cs[i];

                    const pair<string, bool> nstate = make_pair(rev(rev_extra.substr(ss[i].size())), true);
                    if (memo.find(nstate) == memo.end() || memo[nstate] > ncost) {
                        q.emplace(-ncost, nstate);
                        memo[nstate] = ncost;
                    }
                }
            }
        } else {
            for (int i = 0; i < n; ++i) {
                if (rev_ss[i].substr(0, rev_extra.size()) == rev_extra) {
                    const li ncost = cost + cs[i];
                    const pair<string, bool> nstate = make_pair(rev(rev_ss[i].substr(rev_extra.size())), true);
                    if (memo.find(nstate) == memo.end() || memo[nstate] > ncost) {
                        q.emplace(-ncost, nstate);
                        memo[nstate] = ncost;
                    }
                } else if (rev_ss[i].size() < rev_extra.size() && rev_ss[i] == rev_extra.substr(0, rev_ss[i].size())) {
                    const li ncost = cost + cs[i];

                    const pair<string, bool> nstate = make_pair(rev(rev_extra.substr(rev_ss[i].size())), false);
                    if (memo.find(nstate) == memo.end() || memo[nstate] > ncost) {
                        q.emplace(-ncost, nstate);
                        memo[nstate] = ncost;
                    }
                }
            }
        }
    }
    cout << -1 << endl;

    return 0;
}

Measured memory_bytes: 3776