#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;
typedef pair<int, int> sect;
typedef pair<int, sect> P_;
typedef pair<ll, P_> P;

int N;
string S[50];
ll C[50];

ll cost[50][20][20];

set<P> st;

void solve(ll m_){
    ll ans = m_;
    priority_queue<P, vector<P>, greater<P>> que;
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            int mn = min(S[i].size(), S[j].size());
            int mx = max(S[i].size(), S[j].size());
            bool ok = true;
            for(int k = 0; k < mn; k++){
                if(S[i][k] != S[j][S[j].size()-k-1]) ok = false;
            }
            if(ok){
                if(mx <= mn+1){
                    ans = min(ans, C[i]+C[j]);
                }else{
                    ll cost = C[i]+C[j];
                    if(S[i].size() < S[j].size()){
                        sect sct = sect(0, mx-mn-1);
                        que.push(P(cost, P_(j, sct)));
                    }else{
                        sect sct = sect(mn, mx-1);
                        que.push(P(cost, P_(i, sct)));
                    }
                }
            }
        }
    }

    while(!que.empty()){
        P p = que.top(); que.pop();
        sect sct = p.second.second;
        int li = sct.first;
        int ri = sct.second;
        ll cost = p.first;
        ll i = p.second.first;
        // cout << li << ' ' << ri << ' ' << cost << endl;
        bool is_ok = true;
        for(int j = li; j <= ri; j++){
            if(S[i][j] != S[i][ri-(j-li)]) is_ok = false;
        }
        if(is_ok){
            ans = min(ans, cost);
            break;
        }
        for(int j = 0; j < N; j++){
            int mn = min((int)S[j].size(), ri-li+1);
            int mx = max((int)S[j].size(), ri-li+1);
            bool ok = true;
            if(li == 0){
                for(int k = 0; k < mn; k++){
                    if(S[i][ri-k] != S[j][k]) ok = false;
                }
                if(ok){
                    if(mx <= mn+1){
                        ans = min(ans, cost+C[j]);
                    }else{
                        if(S[j].size() > ri-li+1){
                            sect sct = sect(mn, mx-1);
                            que.push(P(cost+C[j], P_(j, sct)));
                        }else{
                            sect sct = sect(li, ri-mn);
                            que.push(P(cost+C[j], P_(i, sct)));
                        }
                    }
                }
            }else{
                // TODO
                // cout << i << ' ' << j << endl;
                for(int k = 0; k < mn; k++){
                    // cout << S[i][li+k] << ' ' << S[j][S[j].size()-k-1] << endl;
                    if(S[i][li+k] != S[j][S[j].size()-k-1]) ok = false;
                }
                if(ok){
                    // cout << i << ':' << j << endl;
                    if(mx <= mn+1){
                        ans = min(ans, cost+C[j]);
                    }else{
                        if(S[j].size() > ri-li+1){
                            sect sct = sect(0, mx-mn-1);
                            que.push(P(cost+C[j], P_(j, sct)));
                        }else{
                            sect sct = sect(li+mn, ri);
                            que.push(P(cost+C[j], P_(i, sct)));
                        }
                    }
                }
            }
        }
    }
    if(ans >= 2e18){
        cout << -1 << endl;
    }else{
        cout << ans << endl;
    }
    
}

bool is_palindrome(string s){
    int sz = s.size();
    for(int i = 0; i < sz; i++){
        if(s[i] != s[sz-i-1]) return false;
    }
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N;
    ll m = 2e18;
    for(int i = 0; i < N; i++){
        cin >> S[i] >> C[i];
        if(is_palindrome(S[i])){
            m = min(C[i], m);
        }
    }
    
    solve(m);
}

Measured memory_bytes: 3672