#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }

const long long INF = 1LL<<60;
int N;
vector<string> S;
vector<long long> C;
using Node = pair<string, int>; // left: 0, right: 1

long long solve() {
    long long res = INF;
    map<Node, long long> dp;
    priority_queue<pair<long long, Node>,
        vector<pair<long long, Node>>, greater<pair<long long, Node>>> que;

    auto func = [&](const string &left, const string &right) {
        int i = (int)left.size()-1, j = 0;
        while (true) {
            if (i == -1 || j == (int)right.size()) break;
            if (left[i] != right[j]) return Node("", -1);
            --i, ++j;
        }
        if (left.size() >= right.size()) {
            string res = left.substr(0, (int)left.size() - (int)right.size());
            return Node(res, 0);
        }
        else {
            string res = right.substr((int)left.size());
            return Node(res, 1);
        }
    };
    auto relax = [&](const string &left, const string &right, long long cost) {
        auto v = func(left, right);
        if (v.second == -1) return;
        if (v.first == "") chmin(res, cost);
        else {
            que.push(make_pair(cost, v));
            if (dp.count(v)) chmin(dp[v], cost);
            else dp[v] = cost;
        }
    };
    
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j <= S[i].size(); ++j) {
            string left = S[i].substr(0, j), right = S[i].substr(j);
            //cout << S[i] << ": " << left << ", " << right << endl;
            relax(left, right, C[i]);
        }
        for (int j = 0; j < S[i].size(); ++j) {
            string left = S[i].substr(0, j), right = S[i].substr(j+1);
            //cout << S[i] << ": " << left << ", " << right << endl;
            relax(left, right, C[i]);
        }
    }
    while (!que.empty()) {
        auto tmp = que.top(); que.pop();
        long long dist = tmp.first;
        string cur = tmp.second.first;
        int type = tmp.second.second;
        if (dist > dp[Node(cur, type)]) continue;

        //COUT(tmp);

        for (int i = 0; i < N; ++i) {
            if (type == 0) {
                string left = cur, right = S[i];
                relax(left, right, dist + C[i]);
            }
            else {
                string left = S[i], right = cur;
                relax(left, right, dist + C[i]);
            }
        }
    }
    return res < INF ? res : -1;
}

int main() {
    while (cin >> N) {
        S.resize(N); C.resize(N);
        for (int i = 0; i < N; ++i) cin >> S[i] >> C[i];
        cout << solve() << endl;
    }
}

















Measured memory_bytes: 3812