#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<string> S;
vector<int> C;
bool f(string s0, string s1, string s2) {
	if (s0.empty()) s0 = s2; else { s1 = s2; reverse(s1.begin(), s1.end()); }
	auto x = min(s0.length(), s1.length());
	return s0.substr(0, x) == s1.substr(0, x);
}
pair<string,string> g(string s0, string s1, string s2) {
	if (s0.empty()) s0 = s2; else { s1 = s2; reverse(s1.begin(), s1.end()); }
	auto x = min(s0.length(), s1.length());
	s0 = s0.substr(x);
	s1 = s1.substr(x);
	auto s = s1;
	reverse(s.begin(), s.end());
	s = s0 + s;
	auto t = s;
	reverse(s.begin(), s.end());
	if (s == t) return {"", ""};
	return {s0, s1};
}
int main() {
	int N;
	cin >> N;
	S.resize(N);
	C.resize(N);
	for (int i = 0; i < N; ++ i) {
		cin >> S[i] >> C[i];
	}
	map<pair<string,string>, ll> dist;
	priority_queue<pair<ll,pair<string,string>>> Q;
	const auto INF = 1LL<<60;
	dist[{"",""}] = INF;
	Q.push({0,{"",""}});
	while (!Q.empty()) {
		auto d0 = -Q.top().first;
		auto s0 = Q.top().second.first;
		auto s1 = Q.top().second.second;
		Q.pop();
		if (d0 != 0 && dist[{s0,s1}] != d0) continue;
		if (s0 == "" && s1 == "" && d0 > 0) {
			cout << d0 << endl;
			return 0;
		} else {
			for (int i = 0; i < N; ++ i) {
				if (f(s0, s1, S[i])) {
					auto p = g(s0, s1, S[i]);
					auto d = d0 + C[i];
					if (!dist.count(p) || d < dist[p]) {
						dist[p] = d;
						Q.push({(ll)-d, p});
					}
				}
			}
		}
	}
	cout << -1 << endl;
}
