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

#define llong long long 
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#define rand __rand
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());  // or mt19937_64
template<class T = int> T rand(T range = numeric_limits<T>::max()) { return (T)(rng() % range); }

#define CONCAT_(x, y) x##y/*{{{*/
#define CONCAT(x, y) CONCAT_(x, y)
#ifdef LOCAL_DEBUG   
int __db_level = 0;
bool __db_same_line = false;
#define clog cerr << string(!__db_same_line ? __db_level * 2 : 0, ' ')
struct debug_block {
    function<void()> fn;
    void print_name() { __db_same_line = true; fn(); clog << endl; __db_same_line = false;  }
    debug_block(function<void()> fn_): fn(fn_) { clog << "{ "; print_name(); ++__db_level; }
    ~debug_block() { --__db_level; clog << "} "; print_name(); }
};
#define DB(args...) debug_block CONCAT(dbbl, __LINE__)([=]{ clog << args; })
#define deb(...)  if (1) { (clog << "[" #__VA_ARGS__ "] = [" << __VA_ARGS__) << "]"; if (!__db_same_line) clog << endl; }
#else
#define clog if (0) cerr
#define DB(...)
#define deb(...)
#endif
template<class T>
ostream& operator,(ostream& out, const T& thing) { return out << ", " << thing; }
template<class U, class V>
ostream& operator<<(ostream& out, const pair<U, V>& p) { return (out << "(" << p.first, p.second) << ")"; }
template<class A, class B>
ostream& operator<<(ostream& out, const tuple<A, B>& t) { return (out << "(" << get<0>(t), get<1>(t)) << ")"; }
template<class A, class B, class C>
ostream& operator<<(ostream& out, const tuple<A, B, C>& t) { return (out << "(" << get<0>(t), get<1>(t), get<2>(t)) << ")"; }
template<class T> ostream& operator<<(ostream& out, const vector<T>& container) { 
    out << "{";
    if (len(container)) out << container[0];
    rep1(i, len(container) - 1) out, container[i];
    return out << "}";
}
template<class x> vector<typename x::value_type> $v(const x& a) { return vector<typename x::value_type>(a.begin(), a.end()); }
#define ptrtype(x) typename iterator_traits<x>::value_type
template<class u> vector<ptrtype(u)> $v(u a, u b) { return vector<ptrtype(u)>(a, b); }/*}}}*/
// ACTUAL SOLUTION BELOW ////////////////////////////////////////////////////////////

const int maxn = 55;
const llong inf = (llong)maxn * INT_MAX + maxn;
int n;
string s[maxn];
llong cost[maxn];

llong min_cost[2][maxn][maxn];

struct node {
    bool type;
    int id;
    int filled;
    llong cost;
    
    node(bool t = 0, int i = 0, int f = 0, llong c = 0)
        : type(t), id(i), filled(f), cost(c) {}
    friend bool operator<(const node& u, const node& v) { return u.cost < v.cost; }
    friend bool operator>(const node& u, const node& v) { return u.cost > v.cost; }
};

int main(void) {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    rep(i, n) cin >> s[i] >> cost[i];
    
    priority_queue<node, vector<node>, greater<node>> prio;
    
    rep(i, n) rep(f, maxn) min_cost[0][i][f] = min_cost[1][i][f] = inf;
    
    rep(i, n) {
        min_cost[0][i][0] = cost[i];
        prio.emplace(0, i, 0, cost[i]);
    }
    
    while (len(prio)) {
        auto [type, u, filled, cur_cost] = prio.top();
        deb(type, u, filled, cur_cost);
        prio.pop();
        if (min_cost[type][u][filled] < cur_cost) continue;
        
        auto su = s[u];
        if (type) reverse(su.begin(), su.end());
        bool is_palin = true;
        int l = filled, r = len(s[u]) - 1;
        for (; l < r; ++l, --r) {
            if (su[l] != su[r]) {
                is_palin = false;
                break;
            }
        }
        if (is_palin) {
            cout << cur_cost;
            return 0;
        }
        
        rep(v, n) {
            string sv = s[v];
            if (!type) reverse(sv.begin(), sv.end());
            l = filled, r = 0;
            
            for (; l < len(su) and r < len(sv); ++l, ++r) {
                if (su[l] != sv[r]) break;
            }
            if (l != len(su) and r != len(sv)) continue;
            
            llong new_cost = cur_cost + cost[v];
            
            int nv = len(su) == l ? v : u;
            int nfilled = len(su) == l ? r : l;
            bool ntype = len(su) == l ? !type : type;
            if (min_cost[ntype][nv][nfilled] <= new_cost) continue;
            min_cost[ntype][nv][nfilled] = new_cost;
            prio.emplace(ntype, nv, nfilled, new_cost);
        }
    }
    
    cout << -1;

    return 0;
}

// Remember:
// - Multitest? REFRESHING the data!!!
// - Constrains for each set of data may differs. Should NOT USE the same max constant (maxn)
//   for all of them.
// vim: foldmethod=marker



Measured memory_bytes: 3704