#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cinttypes>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<stack>


#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vvll=vector<vll>;
using vpii=vector<pii>;

#define PI 3.1415926535897932384626433832795

template<typename X>
bool max_u(X&m, X v)
{
	if(m<v)
	{
		m=v;
		return true;
	}
	return false;
}


template<typename X>
bool min_u(X&m, X v)
{
	if(m>v)
	{
		m=v;
		return true;
	}
	return false;
}


struct part_t
{
	string s, r;
	ll c;
	bool operator<(const part_t&rhs) const
	{
		return c>rhs.c;
	}
};


bool is_ok_i(string&s)
{
	int l=s.length();
	for(int i=l/2;i>=0;i--)
	{
		if(s[i]!=s[l-1-i]) return false;
	}
	return true;
}


bool is_ok(part_t&p)
{
	if(p.s.length()>0)
	{
		return is_ok_i(p.s);
	}
	return is_ok_i(p.r);
}


bool is_match(string&a, string&b)
{
	int l=min(a.length(), b.length());
	for(int i=0;i<l;i++)
	{
		if(a[i]!=b[i]) return false;
	}
	return true;
}


struct solve
{
	solve(){}
	ll operator()(vector<part_t>&p) {
		int n=p.size();
		priority_queue<part_t> q;
		for(auto&m:p)
		{
			part_t c;
			c.s=m.s;
			c.c=m.c;
			q.push(c);
		}
		set<string> hs, hr;
		for(;!q.empty();)
		{
			auto c=q.top();
			q.pop();

//cout << "c.s=[" << c.s << "]  c.r=[" << c.r << "]  c.c=" << c.c << "\n";
			if(is_ok(c)) return c.c;

			if(c.s.length()>0)
			{
				if(hs.find(c.s)!=hs.end()) continue;
				hs.insert(c.s);
				for(int i=0;i<n;i++)
				{
					int pl=p[i].r.length();

					if(is_match(c.s, p[i].r))
					{
						part_t nx;
						nx.c=c.c+p[i].c;
						if(c.s.length()>pl)
						{
							nx.s=c.s.substr(pl);
						}
						else
						{
							nx.r=p[i].r.substr(c.s.length());
						}
						q.push(nx);
					}
				}
			}
			else
			{
				if(hr.find(c.r)!=hr.end()) continue;
				hr.insert(c.r);
				for(int i=0;i<n;i++)
				{
					int pl=p[i].s.length();
					if(is_match(c.r, p[i].s))
					{
						part_t nx;
						nx.c=c.c+p[i].c;
						if(c.r.length()>pl)
						{
							nx.r=c.r.substr(pl);
						}
						else
						{
							nx.s=p[i].s.substr(c.r.length());
						}
						q.push(nx);
					}
				}
			}
		}
		return -1;
	}
};

int main(void)
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	for(;;)
	{
		int n;
		cin >> n;
		if(cin.fail()) break;
		vector<part_t> p(n);
		for(auto&m:p)
		{
			cin >> m.s;
			cin >> m.c;
			m.r=m.s;
			reverse(m.r.begin(), m.r.end());
		}
		cout << solve()(p) << "\n";
	}
	return 0;
}
