#include <bits/stdc++.h>
using namespace std;

using ull = unsigned long long;
using ll = long long;
using ld = long double;

const int mod = 1e9 + 7;
const int inf = INT_MAX;
const int N = 50 + 5;

struct pref {
    int i, l;
    pref(): i(), l() {}
    pref(int i, int l) : i(i), l(l) {}
};

struct suf {
    int i, r;
    suf(): i(), r() {}
    suf(int i, int r) : i(i), r(r) {}
};

struct state {
    ll cost;
    pref p;
    suf s;

    state(): cost(), p(), s() {}
    bool operator>(const state& rhs) {
        return cost > rhs.cost;
    }
};

int n, c[N];
string s[N];
ll ans = LLONG_MAX;

bool _visited[N][20][N][20];
ll _dist[N][20][N][20];

bool& visited(state st) {
    return _visited[st.p.i][st.p.l][st.s.i][st.s.r];
}

ll& dist(state st) {
    return _dist[st.p.i][st.p.l][st.s.i][st.s.r];
}

ll Dijkstra() {
    for (int i = 0; i < N; i++) for (int j = 0; j < 20; j++) for (int k = 0; k < N; k++) for (int l = 0; l < 20; l++) _dist[i][j][k][l] = LLONG_MAX;
    priority_queue<state, vector<state>, greater<>> pq;

    for (int i = 1; i <= n; i++) {
        state _new;
        _new.cost = c[i];
        _new.p = {i, 0};
        _new.s = suf();

        dist(_new) = c[i];
        pq.push(_new);

        _new.p = pref();
        _new.s = {i, (int)s[i].length() - 1};

        dist(_new) = c[i];
        pq.push(_new);
    }

    while (!pq.empty()) {
        state cur = pq.top();
//        cout << cur.cost << " " << cur.p.i << " " << cur.p.l << " " << cur.s.i << " " << cur.s.r << "\n";
        pq.pop();

        if (visited(cur))
            continue;
        visited(cur) = true;

        if (cur.p.i == 0) {
            for (int i = 1; i <= n; i++) {
                int len = min((int)s[i].length(), cur.s.r + 1);
                bool ok = true;

                for (int j = 0; j < len; j++) {
                    ok &= s[i][j] == s[cur.s.i][cur.s.r - j];
                }

                if (ok) {
                    state _new;
                    _new.cost = cur.cost + c[i];
                    if (len == s[i].length())
                        _new.p = pref();
                    else {
                        _new.p.i = i;
                        _new.p.l = len;
                    }

                    if (len == cur.s.r + 1)
                        _new.s = suf();
                    else {
                        _new.s.i = cur.s.i;
                        _new.s.r = cur.s.r - len;
                    }

                    if (_new.p.i) {
                        bool ok = true;
                        for (int j = 0; j < s[_new.p.i].length() - _new.p.l; j++) {
                            ok &= s[_new.p.i][_new.p.l + j] == s[_new.p.i][s[_new.p.i].length() - j - 1];
                        }
                        if (ok) {
                            _new.p = pref();
                            _new.s = suf();
                        }
                    }
                    else if (_new.s.i) {
                        bool ok = true;
                        for (int j = 0; j < _new.s.r + 1; j++) {
                            ok &= s[_new.s.i][j] == s[_new.s.i][_new.s.r - j];
                        }
                        if (ok) {
                            _new.p = pref();
                            _new.s = suf();
                        }
                    }

                    if (_new.cost < dist(_new)) {
                        dist(_new) = _new.cost;
                        pq.push(_new);
                    }
                }
            }
        }
        else {
            for (int i = 1; i <= n; i++) {
                int len = min((int)s[cur.p.i].length() - cur.p.l, (int)s[i].length());
                bool ok = true;

                for (int j = 0; j < len; j++) {
                    ok &= s[cur.p.i][cur.p.l + j] == s[i][s[i].length() - j - 1];
                }

                if (ok) {
                    state _new;
                    _new.cost = cur.cost + c[i];

                    if (len == s[i].length())
                        _new.s = suf();
                    else {
                        _new.s.i = i;
                        _new.s.r = s[i].length() - len - 1;
                    }

                    if (len == s[cur.p.i].length() - cur.p.l)
                        _new.p = pref();
                    else {
                        _new.p.i = cur.p.i;
                        _new.p.l = cur.p.l + len;
                    }

                    if (_new.p.i) {
                        bool ok = true;
                        for (int j = 0; j < s[_new.p.i].length() - _new.p.l; j++) {
                            ok &= s[_new.p.i][_new.p.l + j] == s[_new.p.i][s[_new.p.i].length() - j - 1];
                        }
                        if (ok) {
                            _new.p = pref();
                            _new.s = suf();
                        }
                    }
                    else if (_new.s.i) {
                        bool ok = true;
                        for (int j = 0; j < _new.s.r + 1; j++) {
                            ok &= s[_new.s.i][j] == s[_new.s.i][_new.s.r - j];
                        }
                        if (ok) {
                            _new.p = pref();
                            _new.s = suf();
                        }
                    }

                    if (_new.cost < dist(_new)) {
                        dist(_new) = _new.cost;
                        pq.push(_new);
                    }
                }
            }
        }
    }
    return dist(state());
}

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

    cin >> n;
    for (int i = 1; i <= n; i++) cin >> s[i] >> c[i];

    for (int i = 1; i <= n; i++) {
        bool ok = true;
        for (int j = 0; j < s[i].length(); j++) {
            ok &= s[i][j] == s[i][s[i].length() - j - 1];
        }
        if (ok)
            ans = min(ans, (ll)c[i]);
    }

    ans = min(ans, Dijkstra());
    cout << (ans != LLONG_MAX ? ans : -1) << "\n";
    return 0;
}

Measured memory_bytes: 13996