//@formatter:off
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define rrep(i,n) for (int i = int(n)-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<P>
#define vvp vector<vector<P>>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?']':','); return os; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on

struct edge {
    int to;
    ll len;
    
    edge(int to, ll l) : to(to), len(l) {}
};

vl dijkstra(int start, const vector<vector<edge>> &v) {
    int n = v.size();
    vl dist(n, linf);
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
    q.emplace(0LL, start);
    dist[start] = 0;
    while (!q.empty()) {
        auto p = q.top();
        q.pop();
        if (dist[p.second] < p.first) continue;
        for (auto e : v[p.second]) {
            if (chmin(dist[e.to], p.first + e.len)) {
                q.emplace(p.first + e.len, e.to);
            }
        }
    }
    return dist;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    cin >> n;
    vs s(n);
    vi c(n);
    ll ans = linf;
    rep(i, n) {
        cin >> s[i] >> c[i];
        string rs = s[i];
        reverse(all(rs));
        if (s[i] == rs) chmin(ans, (ll) c[i]);
    }
    auto toId = [](int k, int i, int j) {
        return k * 1000 + i * 20 + j - 1;
    };
    int st = 2000, gl = 2001;
    vector<vector<edge>> G(2002);
    rep(i, n) rep(k, 2) G[st].eb(toId(k, i, s[i].size()), c[i]);
    rep(i, n) rep2(j, 1, s[i].size() + 1) {
            // k == 0
            {
                int now = toId(0, i, j);
                string rem = s[i].substr(s[i].size() - j);
                string A = rem.substr(0, rem.size() / 2);
                string B = rem.substr(rem.size() - rem.size() / 2);
                reverse(all(A));
                if(A == B) G[now].eb(gl,0);
                rep(l, n) {
                    if (s[l].size() < j) {
                        string temp = rem.substr(0, s[l].size());
                        reverse(all(temp));
                        if (temp != s[l]) continue;
                        G[now].eb(toId(0, i, j - s[l].size()), c[l]);
                    } else if (s[l].size() == j) {
                        string temp = rem;
                        reverse(all(temp));
                        if (temp != s[l]) continue;
                        G[now].eb(gl, c[l]);
                    } else {
                        string temp = s[l].substr(s[l].size() - j);
                        reverse(all(temp));
                        if (rem != temp) continue;
                        G[now].eb(toId(1, l, s[l].size() - j), c[l]);
                        temp = s[l].substr(0, s[l].size() - j);
                        A = temp.substr(0, temp.size() / 2);
                        B = temp.substr(temp.size() - temp.size() / 2);
                        reverse(all(A));
                        if (A != B) continue;
                        G[now].eb(gl, c[l]);
                    }
                }
            }
            // k == 1
            {
                int now = toId(1, i, j);
                string rem = s[i].substr(0, j);
                string A = rem.substr(0, rem.size() / 2);
                string B = rem.substr(rem.size() - rem.size() / 2);
                reverse(all(A));
                if(A == B) G[now].eb(gl,0);
                rep(l, n) {
                    if (s[l].size() < j) {
                        string temp = rem.substr(j - s[l].size());
                        reverse(all(temp));
                        if (temp != s[l]) continue;
                        G[now].eb(toId(1, i, j - s[l].size()), c[l]);
                    } else if (s[l].size() == j) {
                        string temp = rem;
                        reverse(all(temp));
                        if (temp != s[l]) continue;
                        G[now].eb(gl, c[l]);
                    } else {
                        string temp = s[l].substr(0, j);
                        reverse(all(temp));
                        if (rem != temp) continue;
                        G[now].eb(toId(0, l, s[l].size() - j), c[l]);
                        temp = s[l].substr(j);
                        A = temp.substr(0, temp.size() / 2);
                        B = temp.substr(temp.size() - temp.size() / 2);
                        reverse(all(A));
                        if (A != B) continue;
                        G[now].eb(gl, c[l]);
                    }
                }
            }
        }
    chmin(ans, dijkstra(st, G)[gl]);
    cout << (ans == linf ? -1 : ans) << endl;
}

Measured memory_bytes: 3744