#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln = '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; }
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; }
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
void print() { cout << "\n"; }
template<class T, class... Args>
void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

ll dp[51][2][21];
using Tu = tuple<ll, int, int, int>;
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N; cin >> N;
    vector<string> S(N+1,"");
    vector<ll> C(N+1);
    rep(i,N) cin >> S[i+1] >> C[i+1];

    rep(i,N+1) rep(j,2) rep(k,21) dp[i][j][k] = 1e18;
    dp[0][0][0] = 0;
    priority_queue<Tu, vector<Tu>, greater<Tu>> pq;
    pq.emplace(Tu(0,0,0,0));
    ll ans = 1e18;
    auto check=[](const string &str) {
        string rts = str;
        reverse(all(rts));
        return rts == str;
    };
    while (!pq.empty()) {
        auto[d,n,r,diff] = pq.top(); pq.pop();
        if (d > dp[n][r][diff]) continue;
        string s = "";
        int L = S[n].size();
        if (!r) {
            s = S[n].substr(L-diff);
            reverse(all(s));
            for (int i = 1; i <= N; i++) {
                int K = S[i].size();
                if (K < diff) {
                    if (S[i]==s.substr(diff-K,K)) {
                        if (check(s.substr(0,diff-K))) chmin(ans,d+C[i]);
                        else {
                            if (chmin(dp[n][0][diff-K],d+C[i])) {
                                pq.emplace(Tu(dp[n][0][diff-K],n,0,diff-K));
                            }
                        }
                    }
                } else if (K > diff) {
                    if (S[i].substr(K-diff,diff)==s) {
                        if (check(S[i].substr(0,K-diff))) chmin(ans,d+C[i]);
                        else {
                            if (chmin(dp[i][1][K-diff],d+C[i])) {
                                pq.emplace(Tu(dp[i][1][K-diff],i,1,K-diff));
                            }
                        }
                    }
                } else {
                    if (S[i]==s) chmin(ans,d+C[i]);
                }
            }
        } else {
            s = S[n].substr(0,diff);
            reverse(all(s));
            for (int i = 1; i <= N; i++) {
                int K = S[i].size();
                if (K < diff) {
                    if (S[i]==s.substr(0,K)) {
                        if (check(s.substr(K,diff-K))) chmin(ans,d+C[i]);
                        else {
                            if (chmin(dp[n][1][diff-K],d+C[i])) {
                                pq.emplace(Tu(dp[n][1][diff-K],n,1,diff-K));
                            }
                        }
                    }
                } else if (K > diff) {
                    if (S[i].substr(0,diff)==s) {
                        if (check(S[i].substr(diff,K-diff))) chmin(ans,d+C[i]);
                        else {
                            if (chmin(dp[i][0][K-diff],d+C[i])) {
                                pq.emplace(Tu(dp[i][0][K-diff],i,0,K-diff));
                            }
                        }
                    }
                } else {
                    if (S[i]==s) chmin(ans,d+C[i]);
                }
            }
        }
    }


    cout << (ans==1e18 ? -1 : ans) << ln;
}

Measured memory_bytes: 3676