#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define inc(i, n)  incID(i, 0, n)
#define dec(i, n)  decID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define FI first
#define SE second
#define FR front()
#define BA back()
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
auto setmin   = [](auto & a, auto b) { return (b <  a ? a = b, true : false); };
auto setmax   = [](auto & a, auto b) { return (b >  a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); };
#define SI(v) static_cast<int>(v.size())
#define RF(e, v) for(auto & e: v)
#define until(e) while(! (e))
#define if_not(e) if(! (e))
#define ef else if
#define UR assert(false)
// 以下、入出力関係のテンプレ案のテスト
auto * IS = & cin;
// input elements (as a tuple)
template<typename U, int I> void in_(U & t) { }
template<typename U, int I, typename A, typename ... B> void in_(U & t) { (* IS) >> get<I>(t); in_<U, I + 1, B ...>(t); }
template<typename ... T> auto in() { tuple<T ...> t; in_<tuple<T ...>, 0, T ...>(t); return t; }
// input a array
template<typename T, int N> auto ain() { array<T, N> a; inc(i, N) { (* IS) >> a[i]; } return a; }
// input a (multi-dimensional) vector
template<typename T> T vin() { return * istream_iterator<T>(* IS); }
template<typename T, typename N, typename ... M> auto vin(N n, M ... m) {
	vector<decltype(vin<T, M ...>(m ...))> v(n); inc(i, n) { v[i] = vin<T, M ...>(m ...); } return v;
}
// input multi-column (as a tuple of vector)
template<typename U, int I> void colin_(U & t) { }
template<typename U, int I, typename A, typename ... B> void colin_(U & t) {
	get<I>(t).emplace_back(* istream_iterator<A>(* IS)); colin_<U, I + 1, B ...>(t);
}
template<typename ... T> auto colin(int n) {
	tuple<vector<T> ...> t; inc(i, n) { colin_<tuple<vector<T> ...>, 0, T ...>(t); } return t;
}
auto * OS = & cout;
string SP = " ", LN = "\n";
// output elements
void out() { (* OS) << LN << flush; }
template<typename A                > void out(A && a            ) { (* OS) << a      ; out(     ); }
template<typename A, typename ... B> void out(A && a, B && ... b) { (* OS) << a << SP; out(b ...); }
// output a (multi-dimensional) vector
template<typename T> ostream & operator<<(ostream & os, vector<T> const & v) {
	inc(i, v.size()) { os << (i == 0 ? "" : SP) << v[i]; } return os << flush;
}
template<typename T> void vout_(T && v) { (* OS) << v; }
template<typename T, typename A, typename ... B> void vout_(T && v, A a, B ... b) {
	for(auto && w: v) { vout_(w, b ...); (* OS) << a; }
}
template<typename T, typename ... A> void vout(T && v, A ... a) { vout_(v, a ...); out(); }

// ---- ----

template<typename T> using LPQ = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using GPQ = priority_queue<T, vector<T>,    less<T>>;

int main() {
	auto [n] = in<int>();
	auto [s, c] = colin<string, LL>(n);
	
	using TS = pair<int, string>;
	LPQ<pair<LL, TS>> q;
	map<TS, LL> mp;
	LL INF = 1e18, ans = INF;
	auto ok = [](string s) { string t = s; reverse(ALL(t)); return (s == t); };
	auto add = [&](TS ts, LL cost, bool flag = true) {
		// if(ts.FI == 0) { out(ts.SE, "--", cost); } else { out("~~", ts.SE, cost); }
		if(flag && ok(ts.SE)) { setmin(ans, cost); return; }
		if(mp[ts] == 0) { mp[ts] = INF; }
		if(setmin(mp[ts], cost)) { q.emplace(cost, ts); }
	};
	add({ 0, "" }, 0, false);
	until(q.empty()) {
		auto [cost, ts] = q.top(); q.pop();
		auto [type, str] = ts;
		inc(i, n) {
			string t, u;
			if(type == 0) { t = str; u = s[i]; }
			else          { t = s[i]; u = str; }
			reverse(ALL(t));
			while(SI(t) > 0 && SI(u) > 0 && t.BA == u.BA) { t.pop_back(); u.pop_back(); }
			reverse(ALL(t));
			if(SI(t) > 0 && SI(u) > 0) { continue; }
			TS nex = (t == "" ? MP(1, u) : MP(0, t));
			add(nex, mp[ts] + c[i]);
		}
	}
	if(ans == INF) { ans = -1; }
	out(ans);
}


Measured memory_bytes: 3652