#include<vector>
#include<map>
#include<algorithm>
#include<set>
#include<iostream>
#include<string>
#include<set>
#include<queue>
#include<random>
#include<unordered_set>

using namespace std;

/*

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;

tree<long long, null_type, less<long long>, rb_tree_tag, tree_order_statistics_node_update> st;

*/

/*

st.find_by_order();
st.order_of_key();

*/

#pragma GCC optimize("Ofast")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")

#define pb push_back
#define ld long double
#define int long long

mt19937 rnd(51);

const int N = 50;
vector<pair<int, pair<int, pair<int, int>>>> go[N][N][2];
int anss[N][N][2];

bool check(string s){
    string t = s;
    reverse(t.begin(), t.end());
    return (s == t);
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    for (int i = 0; i < N; i++){
        for (int j = 0; j < N; j++){
            for (int k = 0; k < 2; k++){
                anss[i][j][k] = -1;
            }
        }
    }
    int n, ans = 1e18;
    cin >> n;
    vector<pair<string, int>> v(n);
    for (int i = 0; i < n; i++){
        cin >> v[i].first >> v[i].second;
        if (check(v[i].first)){
            ans = min(ans, v[i].second);
        }
    }
    for (int i = 0; i < n; i++){
        string s = v[i].first;
        for (int j = 1; j <= s.size(); j++){
            for (int k = 0; k < 2; k++){
                if (k == 0){
                    string t = s.substr(s.size() - j, j);
                    for (int l = 0; l < n; l++){
                        string tt = v[l].first;
                        int ii = 0, jj = tt.size() - 1;
                        while(ii < t.size() && jj >= 0){
                            if (t[ii] != tt[jj]){
                                break;
                            }
                            ii++, jj--;
                        }
                        if (ii == t.size() && jj == -1){
                            go[i][j][k].pb({-1, {-1, {-1, v[l].second}}});
                        }
                        else if (ii == t.size()){
                            string str = v[l].first.substr(0, tt.size() - t.size());
                            if (check(str)){
                                go[i][j][k].pb({-1, {-1, {-1, v[l].second}}});
                                continue;
                            }
                            go[i][j][k].pb({l, {tt.size() - t.size(), {1, v[l].second}}});
                        }
                        else if (jj == -1){
                            string str = v[i].first.substr(v[i].first.size() - (t.size() - tt.size()), (t.size() - tt.size()));
                            if (check(str)){
                                go[i][j][k].pb({-1, {-1, {-1, v[l].second}}});
                                continue;
                            }
                            go[i][j][k].pb({i, {t.size() - tt.size(), {0, v[l].second}}});
                        }
                    }
                }
                else{
                    string t = s.substr(0, j);
                    for (int l = 0; l < n; l++){
                        string tt = v[l].first;
                        int ii = t.size() - 1, jj = 0;
                        while(ii >= 0 && jj < tt.size()){
                            if (tt[jj] != t[ii]){
                                break;
                            }
                            jj++, ii--;
                        }
                        if (jj == tt.size() && ii == -1){
                            go[i][j][k].pb({-1, {-1, {-1, v[l].second}}});
                        }
                        else if (ii == -1){
                            string str = v[l].first.substr(v[l].first.size() - (tt.size() - t.size()), tt.size() - t.size());
                            if (check(str)){
                                go[i][j][k].pb({-1, {-1, {-1, v[l].second}}});
                                continue;
                            }
                            go[i][j][k].pb({l, {tt.size() - t.size(), {0, v[l].second}}});
                        }
                        else if (jj == tt.size()){
                            string str = v[i].first.substr(0, t.size() - tt.size());
                            if (check(str)){
                                go[i][j][k].pb({-1, {-1, {-1, v[l].second}}});
                                continue;
                            }
                            go[i][j][k].pb({i, {t.size() - tt.size(), {1, v[l].second}}});
                        }
                    }
                }
            }
        }
    }
    set<pair<int, pair<int, pair<int, int>>>> st;
    for (int i = 0; i < n; i++){
        for (int j = v[i].first.size(); j <= v[i].first.size(); j++){
            for (auto to : go[i][j][0]){
                st.insert({to.second.second.second + v[i].second, {to.first, {to.second.first, to.second.second.first}}});
            }
        }
    }
    while(!st.empty()){
        auto v = *st.begin();
        st.erase(v);
        int i = v.second.first, j = v.second.second.first, k = v.second.second.second;
        if (i == -1){
            ans = min(ans, v.first);
            break;
        }
        if (anss[i][j][k] == -1){
            anss[i][j][k] = v.first;
        }
        else{
            continue;
        }
        for (auto to : go[i][j][k]){
             st.insert({to.second.second.second + v.first, {to.first, {to.second.first, to.second.second.first}}});
        }
    }
    if (ans == 1e18){
        cout << -1 << endl;
    }
    else{
        cout << ans << endl;
    }
    return 0;
}


Measured memory_bytes: 3940