/**
 *    author:  otera    
**/
#include<iostream>
#include<string> 
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm> 
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
using namespace std;

#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int n;
vector<string> s;
vector<int> c;

using Node = pair<string, int>;

void solve() {
	cin >> n;
    s.resize(n), c.resize(n);
    rep(i, n) {
        cin >> s[i] >> c[i];
    }
    map<pair<string, int>, int> dp;
    priority_queue<pair<int, Node>, vector<pair<int, Node>>, greater<pair<int, Node>>> pque;
    dp[make_pair("", 0)] = 0, dp[make_pair("", 1)] = 0;
    pque.push(make_pair(0, make_pair("", 0)));
    pque.push(make_pair(0, make_pair("", 1)));
    rep(i, n) {
        //prefix
        rep(j, (int)s[i].size()) {
            string t = s[i].substr(0, j + 1);
            reverse(all(t));
            dp[make_pair(t, 0)] = INF;
        }
        //suffix
        rep(j, (int)s[i].size()) dp[make_pair(s[i].substr(j), 1)] = INF;
    }
    int ans = INF;
    while(!pque.empty()) {
        pair<int, Node> p = pque.top(); pque.pop();
        string t = p.sc.fr; int x = p.sc.sc;
        int dist = p.fr;
        if(dist > dp[p.sc]) continue;
        string u = t;
        reverse(all(u));
        if(t == u and (int)t.size() > 0) {
            chmin(ans, dist);
            continue;
        }
        rep(i, n) {
            if(x == 1) reverse(all(s[i]));
            if((int)s[i].size() > (int)t.size()) {
                if(s[i].substr(0, (int)t.size()) == t) {
                    if(chmin(dp[make_pair(s[i].substr((int)t.size()), x ^ 1)], dist + c[i])) {
                        pque.push(make_pair(dist + c[i], make_pair(s[i].substr((int)t.size()), x ^ 1)));
                    }
                }
            } else if((int)s[i].size() == (int)t.size()) {
                if(s[i] == t) {
                    // cerr << s[i] << " " << t << " " << dist + c[i] << endl;
                    chmin(ans, dist + c[i]);
                }
            } else {
                if(t.substr(0, (int)s[i].size()) == s[i]) {
                    if(chmin(dp[make_pair(t.substr((int)s[i].size()), x)], dist + c[i])) {
                        pque.push(make_pair(dist + c[i], make_pair(t.substr((int)s[i].size()), x)));
                    }
                }
            }
            if(x == 1) reverse(all(s[i]));
        }
    }
    if(ans == INF) ans = -1;
    cout << ans << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}

Measured memory_bytes: 3884