#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;

const int N = 105, M = 2020, K = 22;
const long long INF = 0x3f3f3f3f3f3f3f3f;
struct Edge
{
	int v, w;
};
string s[N];
int n, w[N], id[N][K], idx;
long long d[M];
bool st[M];
vector<Edge> g[M];

bool palin(const string& x)
{
	int n = x.size();
	for(int i = 0, j = n - 1; i < j; ++i, --j)
		if(x[i] != x[j]) return false;
	return true;
}

void dijkstra()
{
	typedef pair<long long, int> PII;
	priority_queue<PII, vector<PII>, greater<PII>> h;
	memset(d, 0x3f, sizeof d);
	for(int i = 0; i < n; ++i)
	{
		int u = id[i][s[i].size()];
		d[u] = w[i];
		h.push({d[u], u});
	}
	while(h.size())
	{
		int u = h.top().second;
//		printf("%d\n", u);
		h.pop();
		if(!u) break;
		if(st[u]) continue;
		st[u] = true;
		for(auto& e: g[u])
		{
			int v = e.v, w = e.w;
			if(d[v] > d[u] + w)
			{
				d[v] = d[u] + w;
				h.push({d[v], v});
			}
		}
	}
	return;
}

int main()
{
	cin >> n;
	for(int i = 0; i < n; ++i)
	{
		cin >> s[i] >> w[i];
		s[i + n] = s[i];
		reverse(s[i + n].begin(), s[i + n].end());
		w[i + n] = w[i];
	}
	idx = 1;
	for(int i = 0; i < 2 * n; ++i)
		for(int j = 1; j <= s[i].size(); ++j) id[i][j] = idx++;
	for(int i = 0; i < 2 * n; ++i)
	{
		int l = s[i].size();
		for(int x = 1; x <= l; ++x)
		{
			int u = id[i][x];
			if(palin(s[i].substr(0, x)))
				g[u].push_back({0, 0});
			for(int j = 0; j < 2 * n; ++j) if(i/n != j/n)
			{
				int y = s[j].size(), t = min(x, y);
				if(s[i].substr(x - t, t) != s[j].substr(y - t)) continue;
				if(x < y)
					g[u].push_back({id[j][y - x], w[j]});
				else if(x > y)
					g[u].push_back({id[i][x - y], w[j]});
				else g[u].push_back({0, w[j]});
			}
		}
	}
//	for(int i = 1; i < idx; ++i)
//	{
//		printf("%d:", i);
//		for(int j = 0; j < g[i].size(); ++j)
//			printf("%d ", g[i][j].v);
//		puts("");
//	}

	dijkstra();
	if(d[0] == INF) d[0] = -1;
	cout << d[0] << endl;
	return 0;
}
