#include <bits/stdc++.h>

using namespace std;

#ifdef ACMTUYO
struct RTC{~RTC(){cerr << "Time: " << clock() * 1.0 / CLOCKS_PER_SEC <<" seconds\n";}} runtimecount;
#define DBG(X) cerr << #X << " = " << X << '\n';
#else
struct RTC{};
#define DBG(X)
#endif

#define fast_io() ios_base::sync_with_stdio(false)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define sz(x) ((int)(x).size())
#define all(c) (c).begin(),(c).end()
#define forn(i, n) for (int i = 0; i < (n); i++)
typedef long long int ll;
const int maxs = 51;
const ll inf = LLONG_MAX / 3LL;
int N;
string S[maxs];
ll C[maxs];
int gstart, gend;
vector<int> right_id[maxs], left_id[maxs];
bool is_pal(const string &str, int i, int j) {//[i, j]
  while (i < j) {
    if (str[i] != str[j]) {
      return false;
    }
    i++;
    j--;
  }
  return true;
}
vector<pair<int, ll> > get_edges_right(int a, int i) {
  vector<pair<int,ll> > ret;
  if (is_pal(S[a], i, sz(S[a]) - 1)) {
    ret.eb(gend, 0LL);
    return ret;
  }
  int len_a = sz(S[a]) - i;
  forn (b, N) {
    int r = i, s = sz(S[b]) - 1;
    while (r < sz(S[a]) and s >= 0) {
      if (S[a][r] == S[b][s]) {
        r++;
        s--;
      } else {
        break;
      }
    }
    if (len_a < sz(S[b])) {
      if (r == sz(S[a])) {
        ret.eb(left_id[b][s], C[b]);
      }
    } else if (len_a > sz(S[b])) {
      if (s == -1) {
        ret.eb(right_id[a][r], C[b]);
      }
    } else {//==
      if (r == sz(S[a]) and s == -1) {
        ret.eb(gend, C[b]);
      }
    }
  }
  return ret;
}
vector<pair<int, ll> > get_edges_left(int b, int i) {
  vector<pair<int, ll> > ret;
  if (is_pal(S[b], 0, i)) {
    ret.eb(gend, 0LL);
    return ret;
  }
  int len_b = i + 1;
  forn (a, N) {
    int r = 0, s = i;
    while (r < sz(S[a]) and s >= 0) {
      if (S[a][r] == S[b][s]) {
        r++;
        s--;
      } else {
        break;
      }
    }
    if (sz(S[a]) > len_b) {
      if (s == -1) {
        ret.eb(right_id[a][r], C[a]);
      }
    } else if (sz(S[a]) < len_b) {
      if (r == sz(S[a])) {
        ret.eb(left_id[b][s], C[a]);
      }
    } else { // ==
      if (s == -1 and r == sz(S[a])) {
        ret.eb(gend, C[a]);
      }
    }
  }
  return ret;
}
int main() {
  fast_io();
  cin >> N;
  int next_id = 0;
  forn (i, N) {
    cin >> S[i] >> C[i];
    right_id[i] = vector<int>();
    left_id[i] = vector<int>();
    forn (j, sz(S[i])) {
      right_id[i].pb(next_id++);
      left_id[i].pb(next_id++);
    }
  }
  gstart = next_id++;
  gend = next_id++;

  int n = next_id;
  vector<vector<pair<int, ll> > > g(n, vector<pair<int, ll>>());
  //right
  forn (a, N) {
    forn (i, sz(S[a])) {
      auto rigt_edges = get_edges_right(a, i);
      for (auto e: rigt_edges) {
        g[right_id[a][i]].pb(e);
        //cout << "R " << right_id[a][i] << " -> " << e.first << "(" << e.second << ")" << '\n';
      }
      
      auto left_edges = get_edges_left(a, i);
      for (auto e: left_edges) {
        g[left_id[a][i]].pb(e);
        //cout << "L " << left_id[a][i] << " -> " << e.first << "(" << e.second << ")" << '\n';
      }
    }
    g[gstart].eb(right_id[a][0], C[a]);
  }

  priority_queue<pair<ll, int> > q;
  vector<ll> d(n, inf);
  q.push(mp(0LL, gstart));
  d[gstart] = 0;
  while (!q.empty()) {
    int u; ll du;
    tie(du, u) = q.top();
    q.pop();
    du = -du;
    if (du > d[u]) continue;
    if (u == gend) break;
    for (auto vv: g[u]) {
      int v; ll cost;
      tie(v, cost) = vv;
      if (d[v] > du + cost) {
        d[v] = du + cost;
        q.emplace(-d[v], v);
      }
    }
  }
  if (d[gend] == inf) {
    d[gend] = -1;
  }
  cout << d[gend] << '\n';
  return 0;
}

Measured memory_bytes: 3740