#include <bits/stdc++.h>
using namespace std;

typedef int_fast32_t int32;
typedef int_fast64_t int64;

const int32 inf = 1e9+7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n"  )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define all(obj) (obj).begin(),(obj).end()
#define rall(obj) (obj).rbegin(),(obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32,int32> pii;
typedef pair<int64,int64> pll;

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 == 0 || a > b) { a = b; return true; } return false;
}

bool isPal(string s){
  return s == string(s.rbegin(),s.rend());
}

struct rem{
  string s;
  bool isFront;
  rem(string s):s(s){isFront = true;};
  rem(string s, bool isFront):s(s),isFront(isFront){
    if(s == "")isFront = true;
  };
  bool operator<(const rem& a) const {
    if(s != a.s)return s < a.s;
    return isFront < a.isFront;
  }
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 n;
  cin >> n;
  vector<string> s(n);
  vector<int32> c(n);
  REP(i,n)cin >> s[i] >> c[i];
  priority_queue<pair<int64,rem>,vector<pair<int64,rem>>,greater<pair<int64,rem>>> que;
  map<rem,int64> dist;
  REP(i,n){
    if(chmin(dist[rem(s[i],true)],c[i]))que.emplace(c[i],rem(s[i],true));
    // if(chmin(dist[rem(s[i],false)],c[i]))que.emplace(c[i],rem(s[i],false));
  }
  while(!que.empty()){
    auto [d,r] = que.top();que.pop();
    if(dist[r] != d)continue;
    if(r.s == "")break;
    if(isPal(r.s)){
      chmin(dist[rem("")],d);
      continue;
    }
    // cout << d << " " << r.s << " " << r.isFront << endl;
    REP(i,n){
      if(r.s.size() == s[i].size()){
        if(r.s == string(s[i].rbegin(),s[i].rend())){
          if(chmin(dist[rem("")],d+c[i])){
            que.emplace(dist[rem("")],rem(""));
          }
        }
      }else if(r.s.size() < s[i].size()){
        if(r.isFront){
          if(r.s == string(s[i].rbegin(),s[i].rend()).substr(0,r.s.size())){
            rem nxt(s[i].substr(0,s[i].size()-r.s.size()),false);
            if(chmin(dist[nxt],d+c[i]))que.emplace(dist[nxt],nxt);
          }
        }else{
          if(s[i].substr(0,r.s.size()) == string(r.s.rbegin(),r.s.rend())){
            rem nxt(s[i].substr(r.s.size()),true);
            if(chmin(dist[nxt],d+c[i]))que.emplace(dist[nxt],nxt);
          }
        }
      }else{
        if(r.isFront){
          if(r.s.substr(0,s[i].size()) == string(s[i].rbegin(),s[i].rend())){
            rem nxt(r.s.substr(s[i].size()),true);
            if(chmin(dist[nxt],d+c[i]))que.emplace(dist[nxt],nxt);
          }
        }else{
          if(s[i] == string(r.s.rbegin(),r.s.rend()).substr(0,s[i].size())){
            rem nxt(r.s.substr(0,r.s.size()-s[i].size()),false);
            if(chmin(dist[nxt],d+c[i]))que.emplace(dist[nxt],nxt);
          }
        }
      }
    }
  }
  if(dist[rem("")] == 0)dist[rem("")] = -1;
  ANS(dist[rem("")]);
  // ANS(dist.size());
  // for(auto [r,d] : dist){
  //   cout << r.s << " " << r.isFront << " " << d << endl;
  // }
  return 0;
}

Measured memory_bytes: 3712