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

#define ALL(obj) (obj).begin(),(obj).end()
template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>;

constexpr long long MOD = 1'000'000'000LL + 7; //'
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
vector<string> split(const string &str, const char delemiter) {vector<string> res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;}
int msb(int x) {return x?31-__builtin_clz(x):-1;}
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

bool is_palindrome(const string& s) {
    for(int i = 0; i < s.size()/2; ++i) if(s[i] != s[s.size()-1-i]) return 0;
    return 1;
}

int main() {
    cin.tie(0);ios::sync_with_stdio(false);
    map<string,int> prefix,suffix;

    int N; cin >> N;
    vector<ll> C(N);
    vector<map<string,ll>> dp(2*N);
    priority_queue_reverse<tuple<ll,int,string>> pq;
    for(int i = 0; i < N; ++i) {
        string R; cin >> R >> C[i];
        string S = R;
        reverse(ALL(R));
        if(!prefix.count(S)) prefix[S]=i;
        else {
            int j = prefix[S];
            if(C[i]<C[j]) prefix[S] = i;
        }
        if(!suffix.count(R)) suffix[R]=N+i;
        else {
            int j = suffix[R];
            if(C[i]<C[j]) suffix[R] = i;
        }
        dp[i][S]=C[i];
        pq.push({C[i],i,S});
    }

    ll ans = LOWINF;
    while(pq.size()) {
        auto t = pq.top(); pq.pop();
        ll f_cost = get<0>(t);
        int f_point = get<1>(t);
        string f_rest = get<2>(t);
        
        if(is_palindrome(f_rest)) {
            chmin(ans,f_cost);
            continue;
        }
        if(dp[f_point][f_rest] < f_cost) continue;

        int M = f_rest.size();
        if(f_point < N) {

            for(int i = 1; i <= M; ++i) {
                auto str = f_rest.substr(0,i);
                if(!suffix.count(str)) continue;
                auto& weight = C[suffix[str]%N];

                ll t_cost = f_cost + weight;
                int t_point = f_point;
                string t_rest = f_rest.substr(i);

                if(!dp[t_point].count(t_rest)) dp[t_point][t_rest] = LOWINF;
                if(dp[t_point][t_rest] > t_cost) {
                    dp[t_point][t_rest] = t_cost;
                    pq.push({t_cost,t_point,t_rest});
                }
            }

            for(auto& p:suffix){
                if(f_rest.size() >= p.first.size()) continue;
                if(f_rest != p.first.substr(0,M)) continue;

                auto& weight = C[p.second%N];
                ll t_cost = f_cost + weight;
                int t_point = p.second;
                string t_rest = p.first.substr(M);

                if(!dp[t_point].count(t_rest)) dp[t_point][t_rest] = LOWINF;
                if(dp[t_point][t_rest] > t_cost) {
                    dp[t_point][t_rest] = t_cost;
                    pq.push({t_cost,t_point,t_rest});
                }
            }
        }
        else {

            for(int i = 1; i <= M; ++i) {
                auto str = f_rest.substr(0,i);
                if(!prefix.count(str)) continue;
                auto& weight = C[prefix[str]%N];

                ll t_cost = f_cost + weight;
                int t_point = f_point;
                string t_rest = f_rest.substr(i);

                if(!dp[t_point].count(t_rest)) dp[t_point][t_rest] = LOWINF;
                if(dp[t_point][t_rest] > t_cost) {
                    dp[t_point][t_rest] = t_cost;
                    pq.push({t_cost,t_point,t_rest});
                }
            }

            for(auto& p:prefix){
                if(f_rest.size() >= p.first.size()) continue;
                if(f_rest != p.first.substr(0,M)) continue;

                auto& weight = C[p.second%N];
                ll t_cost = f_cost + weight;
                int t_point = p.second;
                string t_rest = p.first.substr(M);

                if(!dp[t_point].count(t_rest)) dp[t_point][t_rest] = LOWINF;
                if(dp[t_point][t_rest] > t_cost) {
                    dp[t_point][t_rest] = t_cost;
                    pq.push({t_cost,t_point,t_rest});
                }
            }
        }

    }
    if(ans == LOWINF) ans = -1;
    cout << ans << endl;
}

Measured memory_bytes: 3672