// 2020-08-16 10:52:26
#include<bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define REP(i, n, m) for (ll i = (ll)(n); i < (ll)(m); i++)
using namespace std;
using ll = long long;
template<class T> inline bool chmin(T& a, const T& b) {
  if (a > b) { a = b; return true; } else return false;
}

bool is_palindrome(string const& s) {
  return equal(s.begin(), s.end(), s.rbegin());
}

bool is_prefix(string const& s, string const& t) {
  if(s.size() < t.size()) return false;
  return equal(s.begin(), s.begin() + t.size(), t.begin());
}

bool is_suffix(string const& s, string const& t) {
  if(s.size() < t.size()) return false;
  return equal(s.rbegin(), s.rbegin() + t.size(), t.rbegin());
}

string prefix_removed(string const& s, string const& t) {
  return string(s.begin() + t.size(), s.end());
}

string suffix_removed(string const& s, string const& t) {
  return string(s.begin(), s.end() - t.size());
}

const ll inf = 1e18;
int n;
vector<string> S;
vector<ll> C;
ll bfs(int flr, int fi) { // l = 0, r = 1
  using State = pair<string, int>; // u, lr
  using Q = pair<ll, State>; // d, state
  map<State, ll> D; // state, d
  auto update = [&](ll d, State state) {
    if(D.count(state) == 0) D[state] = inf;
    return chmin(D[state], d);
  };
  priority_queue<Q, vector<Q>, greater<Q> > que;
  que.emplace(Q(C[fi], State(S[fi], flr)));
  ll res = inf;
  while(!que.empty()) {
    ll d; State state;
    tie(d, state) = que.top(); que.pop();
    if(!update(d, state)) continue;
    string u; int lr;
    tie(u, lr) = state;
    if(is_palindrome(u)) {
      chmin(res, d);
      continue;
    }
    rep(i, n) {
      string s = u, t = S[i];
      ll nd = d + C[i];
      if(lr == 1) swap(s, t);
      {
        string rt = t;
        reverse(ALL(rt));
        if(is_prefix(s, rt)) {
          string nu(prefix_removed(s, rt));
          que.emplace(Q(nd, State(nu, 0)));
        }
      }
      {
        string rs = s;
        reverse(ALL(rs));
        if(is_suffix(t, rs)) {
          string nu(suffix_removed(t, rs));
          que.emplace(Q(nd, State(nu, 1)));
        }
      }
    }
  }
  return res;
}
void answer() {
  cin >> n;
  rep(i, n) {
    string s;
    ll c;
    cin >> s >> c;
    S.push_back(s);
    C.push_back(c);
  }
  ll ans = inf;
  rep(i, n) {
    chmin(ans, bfs(0, i));
    chmin(ans, bfs(1, i));
  }
  if(ans == inf) ans = -1;
  cout << ans << '\n';
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}