#include <bits/stdc++.h>
using namespace std;
#define LOCAL
#pragma region Macros
typedef long long ll;
#define ALL(x) (x).begin(),(x).end()
const long long MOD=1e9+7;
// const long long MOD=998244353;
const int INF=1e9;
const long long IINF=1e18;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
const char dir[4]={'D','R','U','L'};

template<typename T>
istream &operator>>(istream &is,vector<T> &v){
    for (T &x:v) is >> x;
    return is;
}
template<typename T>
ostream &operator<<(ostream &os,const vector<T> &v){
    for (int i=0;i<v.size();++i){
        os << v[i] << (i+1==v.size()?"": " ");
    }
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const pair<T,U> &p){
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const map<T,U> &m){
    os << '{';
    for (auto itr=m.begin();itr!=m.end();++itr){
        os << '(' << itr->first << ',' << itr->second << ')';
        if (++itr!=m.end()) os << ',';
        --itr;
    }
    os << '}';
    return os;
}
template<typename T>
ostream &operator<<(ostream &os,const set<T> &s){
    os << '{';
    for (auto itr=s.begin();itr!=s.end();++itr){
        os << *itr;
        if (++itr!=s.end()) os << ',';
        --itr;
    }
    os << '}';
    return os;
}

void debug_out(){cerr << '\n';}
template<class Head,class... Tail>
void debug_out(Head&& head,Tail&&... tail){
    cerr << head;
    if (sizeof...(Tail)>0) cerr << ", ";
    debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...) cerr << " ";\
cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n';\
cerr << " ";\
debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

template<typename T> T gcd(T x,T y){return y!=0?gcd(y,x%y):x;}
template<typename T> T lcm(T x,T y){return x/gcd(x,y)*y;}

template<class T1,class T2> inline bool chmin(T1 &a,T2 b){
    if (a>b){a=b; return true;} return false;
}
template<class T1,class T2> inline bool chmax(T1 &a,T2 b){
    if (a<b){a=b; return true;} return false;
}
#pragma endregion

const int MAX_N=55,MAX_S=25;
using P=pair<ll,pair<int,pair<int,int>>>;
ll dp[MAX_N][MAX_S][2];

string pre(string s,int n){return s.substr(0,n);}
string suf(string s,int n){return s.substr((int)s.size()-n,n);}

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

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; cin >> N;
    vector<string> S(N);
    vector<int> C(N),s(N);
    for (int i=0;i<N;++i){
        cin >> S[i] >> C[i];
        s[i]=S[i].size();
    }
    for (int i=0;i<N;++i){
        for (int j=0;j<MAX_S;++j){
            for (int k=0;k<2;++k){
                dp[i][j][k]=IINF;
            }
        }
    }
    priority_queue<P,vector<P>,greater<P>> pq;
    ll ans=IINF;
    auto nxt=[&](int i,int j,int k){pq.push({dp[i][j][k],{i,{j,k}}});};
    for (int i=0;i<N;++i){
        if (check(S[i])) chmin(ans,C[i]);
        for (int j=0;j<=s[i];++j){
            for (int k=0;k<2;++k){
                if (!k){
                    string rest=suf(S[i],s[i]-j);
                    for (int l=0;l<N;++l){
                        if (check(rest+S[l])){
                            chmin(dp[i][j][k],C[i]+C[l]);
                            nxt(i,j,k);
                        }
                    }
                } else {
                    string rest=pre(S[i],s[i]-j);
                    for (int l=0;l<N;++l){
                        if (check(S[l]+rest)){
                            chmin(dp[i][j][k],C[i]+C[l]);
                            nxt(i,j,k);
                        }
                    }
                }
            }
        }
    }
    while(!pq.empty()){
        auto p=pq.top(); pq.pop();
        int i=p.second.first,j=p.second.second.first,k=p.second.second.second;
        if (dp[i][j][k]<p.first) continue;
        if (!j) continue;
        if (!k){
            string rest=pre(S[i],j);
            for (int l=0;l<N;++l){
                string whole=rest+S[l];
                if (j<=s[l]){
                    if (check(pre(whole,2*j))){
                        if (chmin(dp[l][s[l]-j][1],dp[i][j][k]+C[l])){
                            nxt(l,s[l]-j,1);
                        }
                    }
                } else {
                    if (check(suf(whole,2*s[l]))){
                        if (chmin(dp[i][j-s[l]][0],dp[i][j][k]+C[l])){
                            nxt(i,j-s[l],0);
                        }
                    }
                }
            }
        } else {
            string rest=suf(S[i],j);
            for (int l=0;l<N;++l){
                string whole=S[l]+rest;
                if (j<=s[l]){
                    if (check(suf(whole,2*j))){
                        if (chmin(dp[l][s[l]-j][0],dp[i][j][k]+C[l])){
                            nxt(l,s[l]-j,0);
                        }
                    }
                } else {
                    if (check(pre(whole,2*s[l]))){
                        if (chmin(dp[i][j-s[l]][1],dp[i][j][k]+C[l])){
                            nxt(i,j-s[l],1);
                        }
                    }
                }
            }
        }
    }
    for (int i=0;i<N;++i){
        chmin(ans,dp[i][0][0]);
        chmin(ans,dp[i][0][1]);
    }
    cout << (ans==IINF?-1:ans) << '\n';
}

Measured memory_bytes: 3816