#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

ll dp[52][52][41] = {};
constexpr ll inf = 1e18;

bool chmin(ll& a,ll b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vec<string> S(N),T(N);
    vec<ll> C(N);
    vec<int> si(N);
    for(int i=0;i<N;i++){
        cin >> S[i] >> C[i];
        si[i] = S[i].size();
        T[i] = S[i];
        reverse(T[i].begin(),T[i].end());
    }
    int M = 41,mid = 20;
    for(int i=0;i<N;i++) for(int j=0;j<N;j++) for(int k=0;k<M;k++) dp[i][j][k] = inf;
    struct state{
        int left,right,diff;
        ll d;
        bool operator<(const state& R)const{
            return d>R.d;
        }
    };
    priority_queue<state> Q;
    ll ans = inf;
    
    auto is_palindrome = [&](string S){
        string T = S;
        reverse(T.begin(),T.end());
        return S==T;
    };

    for(int i=0;i<N;i++){
        if(is_palindrome(S[i])) ans = min(ans,C[i]);
        Q.push({i,N,si[i]+mid,C[i]});
        dp[i][N][si[i]+mid] = C[i];
    }

    while(!Q.empty()){
        auto [l,r,diff,d] = Q.top(); Q.pop();
        if(dp[l][r][diff]<d) continue;
        if(diff>mid){
            //add right
            diff = diff-mid;
            for(int nr=0;nr<N;nr++){
                int mi = min(diff,si[nr]);
                string s = S[l].substr(si[l]-diff,mi);
                string t = T[nr].substr(0,mi);
                if(s!=t) continue;
                if(diff>si[nr]){
                    int c = diff-si[nr];
                    if(!c || is_palindrome(S[l].substr(si[l]-c,c))){
                        ans = min(ans,d+C[nr]);
                    }else{
                        if(chmin(dp[l][nr][c+mid],d+C[nr])){
                            Q.push({l,nr,c+mid,d+C[nr]});
                        }
                    }
                }else{
                    int c = si[nr]-diff;
                    if(!c || is_palindrome(T[nr].substr(si[nr]-c,c))){
                        ans = min(ans,d+C[nr]);
                    }else{
                        if(chmin(dp[l][nr][-c+mid],d+C[nr])){
                            Q.push({l,nr,-c+mid,d+C[nr]});
                        }
                    }
                }
            }
        }else{
            diff = mid-diff;
            for(int nl=0;nl<N;nl++){
                int mi = min(diff,si[nl]);
                string s = T[r].substr(si[r]-diff,mi);
                string t = S[nl].substr(0,mi);
                if(s!=t) continue;
                if(diff>si[nl]){
                    int c = diff-si[nl];
                    if(!c || is_palindrome(T[r].substr(si[r]-c,c))){
                        ans = min(ans,d+C[nl]);
                    }else{
                        if(chmin(dp[nl][r][-c+mid],d+C[nl])){
                            Q.push({nl,r,-c+mid,d+C[nl]});
                        }
                    }
                }else{
                    int c = si[nl]-diff;
                    if(!c || is_palindrome(S[nl].substr(si[nl]-c,c))){
                        ans = min(ans,d+C[nl]);
                    }else{
                        if(chmin(dp[nl][r][c+mid],d+C[nl])){
                            Q.push({nl,r,c+mid,d+C[nl]});
                        }
                    }
                }
            }
        }
    }
    if(ans==inf) cout << -1 << "\n";
    else cout << ans << "\n";
}

Measured memory_bytes: 4508