#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <string>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <unordered_set>
#include <stack>
#include <queue>
#include <bitset>

#define MOD 1000000007
#define pi 3.14159265358979
#define inf 9223372036854775807
using namespace std;

typedef unsigned long long ull;
typedef long long ll;

bool isPalindrome(string s)
{
	return s == string(s.rbegin(), s.rend());
}

int main()
{
	int n;
	cin >> n;

	vector<string> s(n);
	vector<ll> cost(n);
	
	for (int i = 0; i < n; i++)
	{
		cin >> s[i];
		cin >> cost[i];
	}

	// state = ("string to balance", "prefix or suffix"/"0 or 1")

	ll ans = inf;

	for (int start = 0; start < n; start++)
	{
		map<pair<string, int>, ll> d;
		map<pair<string, int>, bool> used;
		d[{s[start], 0}] = cost[start];
		set<pair<ll, pair<string, int>>> q;
		q.insert({ cost[start], {s[start], 0} });

		while (!q.empty())
		{
			auto state = q.begin()->second;
			q.erase(q.begin());
			if (used.find(state) == used.end())
			{
				if (state.first.size() == 0 || state.first.size() == 1 || isPalindrome(state.first))
				{
					ans = min(ans, d[state]);
				}
				else
				{
					for (int add = 0; add < n; add++)
					{
						if (s[add].size() == state.first.size())
						{
							if (s[add] == string(state.first.rbegin(), state.first.rend()))
							{
								ans = min(ans, d[state] + cost[add]);
							}
						}
						else if (s[add].size() > state.first.size())
						{
							bool ok = true;
							if (state.second == 0)
							{
								string check = string(s[add].rbegin(), s[add].rend());
								for (int i = 0; i < state.first.size(); i++)
								{
									if (state.first[i] != check[i])
									{
										ok = false;
										break;
									}
								}
							}
							else
							{
								string check = string(state.first.rbegin(), state.first.rend());
								for (int i = 0; i < state.first.size(); i++)
								{
									if (s[add][i] != check[i])
									{
										ok = false;
										break;
									}
								}
							}

							if (ok)
							{
								string newString;
								if (state.second == 0)
								{
									newString = s[add].substr(0, s[add].size() - state.first.size());
								}
								else
								{
									newString = s[add].substr(state.first.size());
								}
								auto newState = make_pair(newString, (state.second + 1) % 2);

								if (d.find(newState) == d.end())
								{
									d[newState] = d[state] + cost[add];
									q.insert(make_pair(d[newState], newState));
								}
								else
								{
									if (d[newState] > d[state] + cost[add])
									{
										if (q.find(make_pair(d[newState], newState)) != q.end())
											q.erase(make_pair(d[newState], newState));
										d[newState] = d[state] + cost[add];
										q.insert(make_pair(d[newState], newState));
									}
								}
							}

						}
						else
						{
							bool ok = true;
							if (state.second == 0)
							{
								string check = string(s[add].rbegin(), s[add].rend());
								for (int i = 0; i < check.size(); i++)
								{
									if (state.first[i] != check[i])
									{
										ok = false;
										break;
									}
								}
							}
							else
							{
								string check = string(state.first.rbegin(), state.first.rend());
								for (int i = 0; i < s[add].size(); i++)
								{
									if (s[add][i] != check[i])
									{
										ok = false;
										break;
									}
								}
							}

							if (ok)
							{
								string newString;
								if (state.second == 0)
								{
									newString = state.first.substr(s[add].size());
								}
								else
								{
									newString = state.first.substr(0, state.first.size() - s[add].size());
								}
								auto newState = make_pair(newString, state.second);

								if (d.find(newState) == d.end())
								{
									d[newState] = d[state] + cost[add];
									q.insert(make_pair(d[newState], newState));
								}
								else
								{
									if (d[newState] > d[state] + cost[add])
									{
										if (q.find(make_pair(d[newState], newState)) != q.end())
											q.erase(make_pair(d[newState], newState));
										d[newState] = d[state] + cost[add];
										q.insert(make_pair(d[newState], newState));
									}
								}
							}
						}
					}
				}
				used[state] = true;
			}
		}

	}
	
	if (ans == inf)
		cout << -1;
	else
		cout << ans;

	return 0;
}


Measured memory_bytes: 3240