#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }

template <class T, size_t I, size_t... J>
struct ArrType { using t = std::array<typename ArrType<T, J...>::t, I>; };
template <class T, std::size_t I>
struct ArrType<T, I> { using t = std::array<T, I>; };
template <class T, std::size_t N, std::size_t... M>
using Arr = typename ArrType<T, N, M...>::t;
// 使うときはstaticをつける

template <typename T>
struct edge {
    int from, to; T cost;
    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
};

template <class K, class V>
class RadixHeap {
    static constexpr int bit_length = sizeof(K)*8;
    K last;
    size_t sz, cnt;

    array<vector<pair<K, V>>, bit_length> v;
    static inline int bsr(int x){
        return x ? bit_length-__builtin_clz(x) : 0;
    }
    static inline int bsr(ll x){
        return x ? bit_length-__builtin_clzll(x) : 0;
    }

    void pull() {
        if(cnt < v[0].size()) return;;
        int i = 1;
        while(v[i].empty()) i++;
        last = min_element(v[i].begin(),v[i].end())->first;
        for (auto &&x : v[i]) v[bsr(x.first ^ last)].push_back(x);
        v[i].clear();
    }
public:
    RadixHeap() : last(0), sz(0), cnt(0) {}
    void emplace(K x, V val){
        sz++;
        v[bsr(x^last)].emplace_back(x, val);
    }

    pair<K, V> top() {
        pull();
        return v[0][cnt];
    }

    void pop() {
        pull();
        sz--;
        cnt++;
    }

    size_t size() const { return sz; }
    bool empty() const { return !sz; }
};

bool is_parindrome(string s, char c = '?'){
    auto n = s.size();
    for (int i = 0; i < (n+1)/2; ++i) {
        if(s[i] == c || s[n-i-1] == c) continue;
        if(s[i] != s[n-i-1]) return false;
    }
    return true;
}


int main() {
    int n;
    cin >> n;
    vector<string> s(n), t(n);
    vector<int> cs(n);
    for (int i = 0; i < n; ++i) {
        cin >> s[i] >> cs[i];
        t[i] = s[i];
        reverse(t[i].begin(),t[i].end());
    }
    static Arr<ll, 50, 51, 50, 51> dp;
    for (auto &&i : dp) for (auto &&j : i) for (auto &&k : j) fill(k.begin(),k.end(), INF<ll>);
    RadixHeap<ll, tuple<int, int, int, int>> Q;
    ll ans = INF<ll>;
    for (int i = 0; i < n; ++i) {
        if(is_parindrome(s[i])) chmin<ll>(ans, cs[i]);
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if(s[i][0] != t[j][0]) continue;
            dp[i][1][j][1] = cs[i]+cs[j];
            Q.emplace(cs[i]+cs[j], {i, 1, j, 1});
        }
    }
    vector<vector<int>> maes(n, vector<int>(51, 0));
    auto maet = maes;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < s[i].size(); ++j) {
            maes[i][j] = is_parindrome(string(s[i].begin()+j,s[i].end()));
            maet[i][j] = is_parindrome(string(t[i].begin()+j,t[i].end()));
        }
    }
    while(!Q.empty()){
        ll cost; tuple<int, int, int, int> tt;
        tie(cost, tt) = Q.top(); Q.pop();
        int a, b, c, d; tie(a, b, c, d) = tt;
        if(dp[a][b][c][d] < cost) continue;
        if(b == s[a].size() && d == t[c].size()){
            chmin(ans, cost);
        } else if(b == s[a].size()){
            if(maet[c][d]) chmin(ans, cost);
            else {
                for (int i = 0; i < n; ++i) {
                    if(t[c][d] == s[i][0]){
                        if(dp[i][1][c][d+1] <= cost + cs[i]) continue;
                        dp[i][1][c][d+1] = cost + cs[i];
                        Q.emplace(cost + cs[i], {i, 1, c, d+1});
                    }
                }
            }
        }else if(d == t[c].size()){
            if(maes[a][b]) chmin(ans, cost);
            else {
                for (int i = 0; i < n; ++i) {
                    if(t[i][0] == s[a][b]){
                        if(dp[a][b+1][i][1] <= cost + cs[i]) continue;
                        dp[a][b+1][i][1] = cost + cs[i];
                        Q.emplace(cost + cs[i], {a, b+1, i, 1});
                    }
                }
            }
        }else {
            if(s[a][b] == t[c][d]){
                if(dp[a][b+1][c][d+1] <= cost) continue;
                dp[a][b+1][c][d+1] = cost;
                Q.emplace(cost, {a, b+1, c, d+1});
            }
        }
    }
    cout << (ans == INF<ll> ? -1 : ans) << "\n";
    return 0;
}

Measured memory_bytes: 54668