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

const int N = 55;
const ll inf = 1e18;

string str[N];
ll w[N];
int n;

typedef pair<string, int> point;
map<point, ll> dis;

ll getdis(const point& s) {
    if (dis.count(s)) return dis[s];
    return inf;
}

void solve() {
    scanf("%d", &n);

    queue<point> que;

    ll ans = inf;
    for (int k = 1; k <= n; k++) {
        cin >> str[k] >> w[k];
        int len = str[k].size();
        for (int i = 0; i < len; i++) {
            int l = i-1, r = i+1;
            while (l >= 0 && r < len && str[k][l] == str[k][r]) {
                l--, r++;
            }
            if (l >= 0 && r < len) continue;
            if (l < 0 && r >= len) {
                ans = min(ans, w[k]);
                continue;
            }
            if (l >= 0) {
                auto s = point(str[k].substr(0, l+1), 0);
                ll curdis = getdis(s);
                dis[s] = min(curdis, w[k]);
                que.push(s);
            } else {
                auto s = point(str[k].substr(r, len), 1);
                ll curdis = getdis(s);
                dis[s] = min(curdis, w[k]);
                que.push(s);
            }
        }

        for (int i = 0; i <= len; i++) {
            int l = i-1, r = i;
            while (l >= 0 && r < len && str[k][l] == str[k][r]) {
                l--, r++;
            }
            if (l >= 0 && r < len) continue;
            if (l < 0 && r >= len) {
                ans = min(ans, w[k]);
                continue;
            }
            if (l >= 0) {
                auto s = point(str[k].substr(0, l+1), 0);
                ll curdis = getdis(s);
                dis[s] = min(curdis, w[k]);
                que.push(s);
            } else {
                auto s = point(str[k].substr(r, len), 1);
                ll curdis = getdis(s);
                dis[s] = min(curdis, w[k]);
                que.push(s);
            }
        }
    }

    while (!que.empty()) {
        const point u = que.front();
        que.pop();
        if (u.first == "") continue;
        ll disu = getdis(u);
        //cout << "que " << u.first << ' ' << u.second << ' ' << disu << endl;
        for (int i = 1; i <= n; i++) {
            string cur;
            int l, r;
            if (u.second == 0) {
                cur = u.first + str[i];
                l = u.first.size()-1;
                r = u.first.size();
            } else {
                cur = str[i] + u.first;
                l = str[i].size()-1;
                r = str[i].size();
            }
            int len = cur.size();
            while (l >= 0 && r < len && cur[l] == cur[r]) l--, r++;
            if (l >= 0 && r < len) continue;
            ll curdis = disu + w[i];
            if (l < 0 && r >= len) {
                //cout << u.first << ' ' << u.second << ' ' << str[i] << endl;
                ans = min(ans, curdis);
                continue;
            }

            if (l >= 0) {
                auto s = point(cur.substr(0, l+1), 0);
                if (getdis(s) > curdis) {
                    que.push(s);
                    dis[s] = curdis;
                }
            } else {
                auto s = point(cur.substr(r, len), 1);
                if (getdis(s) > curdis) {
                    que.push(s);
                    dis[s] = curdis;
                }
            }
        }
    }
    if (ans == inf) ans = -1;
    printf("%lld\n", ans);

}

int main() {
    int T = 1;
    // scanf("%d", &T);
    while (T--) {
        solve();
    }
    return 0;
}


Measured memory_bytes: 3952