#ifndef _TEMPLATE_ROOT
#define _TEMPLATE_ROOT
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define repl(i, a, b) for(ll i = a; i < (b); ++i)
#define repd(i, a, b) for(int i = a; i >= (b); --i)
#define repdl(i, a, b) for(ll i = a; i >= (b); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template<typename H> bool chmin(H& v1, const H v2) { if (v1 > v2) { v1 = v2; return true; } return false; }
template<typename H> bool chmax(H& v1, const H v2) { if (v1 < v2) { v1 = v2; return true; } return false; }
#endif
#ifndef _TEMPLATE_IO
#define _TEMPLATE_IO

template<typename H> void read(H& head) { cin >> head; }
template<typename H, typename ...T> void read(H& head, T& ...tail) { cin >> head; read(tail...); }
template<typename H> void write(H head) { cout << head << '\n'; }
template<typename H, typename ...T> void write(H head, T ...tail) { cout << head << " "; write(tail...); }
template<typename ...T> void writef(T ...tail) { write(tail...); cout << flush; }
template<typename ...T> void die(T ...tok) { write(tok...); exit(0); }
template<typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
  if (v.size()) {
    rep(i, 0, sz(v) - 1)
      out << v[i] << " ";
    out << v.back();
  }
  return out;
}
#endif
using namespace std;
const ll infll = 0x3f3f3f3f3f3f3f3fll;

int P = 0, S = 1;

int enc(int ps, int i, int j) {
  return (ps << 11) | (i << 5) | j;
}

int n;
string s[55];
ll c[55];
bool is_end[1 << 12];
ll dist[1 << 12];
vector<pair<ll, int>> g[1 << 12];

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);

  // end state: 0
  read(n);
  rep(i, 1, n + 1) read(s[i], c[i]);

  rep(i, 0, 1 << 12) dist[i] = infll;

  // initial state
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
  rep(i, 1, n + 1) {
    int pre = enc(P, i, sz(s[i])), suf = enc(S, i, sz(s[i]));
    dist[pre] = dist[suf] = c[i];
    pq.push({ dist[pre], pre });
    pq.push({ dist[suf], suf });
  }

  rep(i, 1, n + 1) {
    rep(j, 1, sz(s[i]) + 1) {
      // auto transitions
      {
        if (string(s[i].end() - j, s[i].end()) == string(s[i].rbegin(), s[i].rbegin() + j)) {
          g[enc(P, i, j)].push_back({ 0, 0 });
        }
        if (string(s[i].begin(), s[i].begin() + j) == string(s[i].rend() - j, s[i].rend())) {
          g[enc(S, i, j)].push_back({ 0, 0 });
        }
      }

      rep(k, 1, n + 1) {
        // prefix transitions
        {
          int l = min(j, sz(s[k]));
          string cuti = string(s[i].end() - j, s[i].end() - j + l);
          string cutj = string(s[k].rbegin(), s[k].rbegin() + l);
          if (cuti == cutj) {
            // possible transition
            int target;
            if (j == s[k].size()) {
              target = 0;
            } else if (l < j) {
              target = enc(P, i, j - l);
            } else {
              target = enc(S, k, sz(s[k]) - l);
            }
            g[enc(P, i, j)].push_back({ c[k], target });
          }
        }

        // suffix transitions
        {
          int l = min(j, sz(s[k]));
          string cuti = string(s[i].rend() - j, s[i].rend() - j + l);
          string cutj = string(s[k].begin(), s[k].begin() + l);
          if (cuti == cutj) {
            int target;
            if (j == s[k].size()) {
              target = 0;
            } else if (l < j) {
              target = enc(S, i, j - l);
            } else {
              target = enc(P, k, sz(s[k]) - l);
            }
            g[enc(S, i, j)].push_back({ c[k], target });
          }
        }
      }
    }
  }

  while (!pq.empty()) {
    ll cdist; int pos;
    tie(cdist, pos) = pq.top(); pq.pop();
    if (cdist > dist[pos]) continue;
    for (auto& p : g[pos]) {
      ll cost; int target;
      tie(cost, target) = p;
      if (dist[target] > dist[pos] + cost) {
        dist[target] = dist[pos] + cost;
        pq.push({ dist[target], target });
      }
    }
  }

  write(dist[0] != infll ? dist[0] : -1ll);
}



Measured memory_bytes: 3872