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

typedef long long ll;

const ll N = 55, inf = 1e17;

ll cost[N], n;

string str[N];


bool is_pallindrome(string s) {
	string t = s;
	reverse(t.begin(), t.end());
	return t == s;
}


ll dp[52][21][2][2][10001];




ll go(ll idx, ll len, ll pref, ll suff, ll dep) {
	if (len <= 1) {
		return 0;
	}
	if (pref && is_pallindrome(str[idx].substr(0, len))) {
		return 0;
	}
	if (suff && is_pallindrome(str[idx].substr(str[idx].size() - len, len))) {
		return 0;
	}
	if (dep > 10000) {
		return inf;
	}
	ll &ret = dp[idx][len][pref][suff][dep];
	if (~ret) {
		return ret;
	}
	ret = inf;
	for (ll i = 1; i <= n; i++) {
		if (pref) {
			ll k = min(len, (ll)str[i].size());
			if (is_pallindrome(str[i].substr(0, k) + str[idx].substr(len - k, k))) {
				if (k < (int) str[i].size()) {
					ret = min(ret, cost[i] + go(i, str[i].size() - k, 0, 1, dep + 1));
				} else {
					ret = min(ret, cost[i] + go(idx, len - k, 1, 0, dep + 1));
				}
			}
		} else {
			ll k = min(len, (ll)str[i].size()), pos = str[idx].size() - len;
			if (is_pallindrome(str[idx].substr(pos, k) + str[i].substr(str[i].size() - k, k))) {
				if (k < (int) str[i].size()) {
					ret = min(ret, cost[i] + go(i, str[i].size() - k, 1, 0, dep + 1));
				} else {
					ret = min(ret, cost[i] + go(idx, len - k, 0, 1, dep + 1));
				}
			}
		}
	}
	return ret;
}

int32_t main() {
	/*
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	freopen("error.txt", "w", stderr);
#endif
	//*/

	cin >> n;
	ll ans = inf;
	for (ll i = 1; i <= n; i++) {
		cin >> str[i] >> cost[i];
		if (is_pallindrome(str[i])) {
			ans = min(ans, cost[i]);
		}
	}
	memset(dp, -1, sizeof dp);
	for (ll i = 1; i <= n; i++) {
		for (ll j = 1; j <= n; j++) {
			ll n = str[i].size(), m = str[j].size();
			if (n <= m) {
				if (is_pallindrome(str[i] + str[j].substr(m - n, n))) {
					ans = min(ans, cost[i] + cost[j] + go(j, m - n, 1, 0, 1));
				}
			} else {
				if (is_pallindrome(str[i].substr(0, m) + str[j])) {
					ans = min(ans, cost[i] + cost[j] + go(i, n - m, 0, 1, 1));
				}
			}
		}
	}
	if (ans >= inf) ans = -1;
	cout << ans << endl;
}