#include "bits/stdc++.h"
#define FI first.first
#define SE first.second
#define TH second
#define fi first
#define se second
#define th second

using namespace std;

template<typename T>
string to_string(const vector<T>& vc, int w) {
	if(vc.empty()) return "";
	if(w + 1 == vc.size()) return to_string(vc[w]);
	return to_string(vc[w]) + "," + to_string(vc, w + 1);
}

template<typename T>
string to_string(const vector<T>& vc) {
	return "{" + to_string(vc, 0) + "}";
}

void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
 
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

class DebugStream {}LOG;
template <typename T>DebugStream &operator<<(DebugStream &s, const T&) { return s; }
#ifdef DEBUG
#define LOG clog
#endif

typedef long long ll;
typedef pair<ll, ll> ii;
typedef long double ld;
typedef vector<string> VS;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

const int maxN = 1e5 + 9, maxV = 1e6 + 9, MOD = 1e9 + 7, SQ = 335, lg = 20, bs = 29;

map<vector<string>, int> mp;
string sts[maxN];
vector<string> otr[maxN];
vector<ii> adj[maxN];
ll csts[maxN];
int n, ctr = 1;

vector<string> trans(vector<string> vc, string str, int sd) {
	if(vc[sd ^ 1].size()) return vector<string>();
	auto& cr = vc[sd];
	while(cr.size() && str.size()) {
		if(cr.back() != str.back()) return vector<string>();
		cr.pop_back();
		str.pop_back();
	}
	vc[sd ^ 1] = str;
	return vc;
}

vector<vector<string>> getall(string str) {
	vector<vector<string>> ret;
	for(int i = 0; i < str.size(); i++) {
		for(int u = 0; u < 2; u++) {
			string prf = str.substr(0, i), suf = str.substr(i + u);
			reverse(suf.begin(), suf.end());
			auto uno = vector<string>({prf, ""});
			auto dos = vector<string>({"", suf});
			auto urp = trans(uno, suf, 0);
			if(urp.size()) ret.push_back(urp);
			auto drp = trans(dos, prf, 1);
			if(drp.size()) ret.push_back(drp);
		}
	}
	return ret;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	#ifdef DEBUG
	freopen("input.txt", "r", stdin);
	#endif

	cin >> n;
	vector<string> tran;
	vector<int> sds;
	vector<ll> tc;
	for(int i = 0; i < n; i++) {
		string str;
		ll cst;
		cin >> str >> cst;
		sts[i] = str;
		csts[i] = cst;
		for(int j = 1; j <= str.size(); j++)
			mp[vector<string>({str.substr(0, j), ""})];
		tran.push_back(str);
		sds.push_back(1);
		tc.push_back(cst);
		reverse(str.begin(), str.end());
		for(int j = 1; j <= str.size(); j++)
			mp[vector<string>({"", str.substr(0, j)})];
		tran.push_back(str);
		sds.push_back(0);
		tc.push_back(cst);
	}
	mp[vector<string>({"", ""})];

	for(auto& el: mp) {
		otr[ctr] = el.fi;
		el.se = ctr++;
	}

	for(int i = 0; i < n; i++) {
		auto all = getall(sts[i]);
		for(auto vcs: all) {
			int w = mp[vcs];
			adj[0].push_back(ii(csts[i], w));
		}
	}

	for(int i = 0; i < ctr; i++) {
		if(otr[i].size() != 2 || otr[i] == vector<string>({"", ""})) continue;
		for(int j = 0; j < tran.size(); j++) {
			auto resp = trans(otr[i], tran[j], sds[j]);
			if(resp.size()) adj[i].push_back(ii(tc[j], mp[resp]));
		}
	}

	priority_queue<ii, vector<ii>, greater<ii>> pq;
	pq.push(ii(0, 0));
	vector<bool> vi(ctr);
	vector<ll> respu(ctr, -1);

	while(pq.size()) {
		auto cr = pq.top();
		pq.pop();
		int nd = cr.se;
		if(vi[nd]) continue;
		respu[nd] = cr.fi;
		vi[nd] = 1;
		for(auto el: adj[nd]) {
			if(vi[el.se]) continue;
			pq.push(ii(cr.fi + el.fi, el.se));
		}
	}

	cout << respu[mp[vector<string>({"", ""})]] << '\n';
}



Measured memory_bytes: 11948