/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include<bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

class FMakingPalindrome {
public:

    void solve(istream& cin, ostream& cout) {
      SPEED;
      int n; cin >> n;
      vector<string> s(n);
      vector<ll> c(n);
      rep(i, n) {
        cin >> s[i] >> c[i];
      }

      ll dp[n*21*2];
      // k = 0: suffix
      // k = 1: prefix
      // j: length
      rep(i, n*21*2) {
        dp[i] = LLONG_MAX/4;
      }

      priority_queue<P, vector<P>, greater<>> que;

      rep(i, n) {
        int len = s[i].size();
        debug(i*42+len*2, c[i]);
        debug(i*42+len*2+1, c[i]);
        dp[i*42+len*2] = dp[i*42+len*2+1] = c[i];
        que.emplace(c[i], i*42+len*2);
        que.emplace(c[i], i*42+len*2+1);
      }

      vector<P> graph[n*21*2];
      rep(i, n) {
        rep(j, n) {
          for(int k=1; k<=s[i].size(); k++) {
            for(int l=1; l<=s[j].size(); l++) {
              if (k < s[i].size() && l < s[j].size()) continue;
              // s[i]: suffix len k
              // s[j]: prefix len l
              int mi = min(k, l);
              bool flag = true;
              int ip = s[i].size()-k;
              int jp = l-1;
              for(int p=0; p<mi; p++, ip++, jp--) {
                if (s[i][ip] != s[j][jp]) {
                  flag = false;
                  break;
                }
              }
              if (!flag) continue;

              if (l == s[j].size()) {
                int from = i*42+k*2;
                int to;
                if (k >= l) {
                  to = i*42+(k-l)*2;
                } else {
                  to = j*42+(l-k)*2+1;
                }
                debug(from, to, c[j]);
                graph[from].emplace_back(to, c[j]);
              }
              if (k == s[i].size()) {
                int from = j*42+l*2+1;
                int to;
                if (k >= l) {
                  to = i*42+(k-l)*2;
                } else {
                  to = j*42+(l-k)*2+1;
                }
                debug(from, to, c[i]);
                graph[from].emplace_back(to, c[i]);
              }
            }
          }
        }
      }

      while(!que.empty()) {
        auto p = que.top(); que.pop();
        if (p.first > dp[p.second]) continue;
        for(const auto& e: graph[p.second]) {
          if (dp[e.first] > p.first + e.second) {
            dp[e.first] = p.first + e.second;
            debug(dp[e.first], e.first);
            que.emplace(dp[e.first], e.first);
          }
        }
      }

      ll ret = LLONG_MAX/4;
      rep(i, n*21*2) {
        int idx = i/42;
        int len = (i%42)/2;
        string& t = s[idx];
        if (t.size() < len) continue;

        int k = i%2;
        int start, end;
        if (k == 0) {
          // suffix
          start = t.size()-len;
          end = t.size()-1;
        } else {
          start = 0;
          end = len-1;
        }
        bool flag = true;
        while(start <= end) {
          if (t[start] != t[end]) {
            flag = false;
            break;
          }
          start++; end--;
        }
        if (flag) {
          if (chmin(ret, dp[i])) {
            debug(i, idx, len, k, dp[i]);
          }
        }
      }
      debug(ret);

      if (ret == LLONG_MAX/4) {
        cout << -1 << endl;
      } else {
        cout << ret << endl;
      }


    }
};

signed main() {
  FMakingPalindrome solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}

Measured memory_bytes: 3752