#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Vll = vector<ll>;
using V = vector<int>;
using VV = vector<V>;
using VVV = vector<VV>;
using Pll = pair<ll, ll>;
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define all(a) (a).begin(), (a).end()
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template<typename T> inline int sz(T &x) { return x.size(); }
template<typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }
const ll INFll = 1ll<<60;


template<typename T>
struct edge {
  int v; T w;
  edge(int v, T w) : v(v), w(w) {}
};
template<typename T>
using Graph = vector<vector<edge<T>>>;

string rev(string s) { reverse(all(s)); return s; }

map<string, int> idS[2];
vector<string> s[2];


int main() {
  int n; cin >> n;
  Vll c(n);
  rep(i, n) {
    string x;
    cin >> x >> c[i];
    s[0].push_back(x);
    s[1].push_back(rev(x));
  }

  int N = 0;
  VVV nid(2, VV(n));
  rep(r, 2) rep(i, n) rep(j, sz(s[r][i])+1) nid[r][i].push_back(N++);

  Graph<ll> G(N);
  rep(r, 2) rep(i, n) rep2(j, 1, sz(s[r][i])+1) rep(k, n) {
    auto s1 = rev(s[r][i].substr(0, j));
    auto s2 = s[r][k];

    int l = sz(s2);
    int mn = min(j, l);
    if (s1.substr(0, mn) != s2.substr(0, mn)) continue;
    
    int r2 = r;
    int i2 = i;
    int j2 = abs(j-l);
    if (j < l) r2 ^= 1, i2 = k;
    G[nid[r][i][j]].emplace_back(nid[r2][i2][j2], c[k]);
  }

  Vll dist(N, INFll);
  priority_queue<Pll, vector<Pll>, greater<Pll>> pq;
  rep(i, n) {
    int x = nid[0][i][sz(s[0][i])];
    dist[x] = c[i];
    pq.emplace(dist[x], x);
  } 
  while (!pq.empty()) {
    auto [d, u] = pq.top(); pq.pop();
    if (dist[u] < d) continue;
    for (auto &[v, w] : G[u]) {
      if (chmin(dist[v], d+w)) pq.emplace(dist[v], v);
    }
  }

  ll ans = INFll;
  rep(r, 2) rep(i, n) rep(j, sz(s[r][i])+1) {
    auto t = s[r][i].substr(0, j);
    if (t == rev(t)) chmin(ans, dist[nid[r][i][j]]);
  }
  cout << (ans == INFll ? -1 : ans) << '\n';
}
