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

typedef long long ll;
typedef pair <int , int> pii;
typedef long double ld;

const int N = 51 , mod = 1e9+7;

string s[N];
int c[N];
vector <int> g[N][N][2];
bool p[N][N][2];
set < pair < pair <ll , int> , pii> > st;
ll ans = 1ll * mod * mod , dis[N][N][2];

void dijekstra(){
   while(st.size()){
      auto x = *st.begin();
      st.erase(st.begin());
      ll d = x.first.first;
      if(d >= ans)
         break;
      int v = x.first.second;
      int len = x.second.first;
      int pos = x.second.second;
      if(p[v][len][pos]){
         ans = d;
         break;
      }
      for(int u : g[v][len][pos]){
         if(abs(len - int(s[u].size())) <= 1){
            ans = min(ans , d + c[u]);
         }
         else if(len > s[u].length()){
            if(dis[v][len - s[u].length()][pos] > d + c[u]){
               st.erase({{dis[v][len - s[u].length()][pos] , v} , {len - s[u].length() , pos}});
               dis[v][len - s[u].length()][pos] = d + c[u];
               st.insert({{dis[v][len - s[u].length()][pos] , v} , {len - s[u].length() , pos}});
            }
         }
         else{
            if(dis[u][s[u].length() - len][1 - pos] > d + c[u]){
               st.erase({{dis[u][s[u].length() - len][1 - pos] , u} , {s[u].length() - len , 1 - pos}});
               dis[u][s[u].length() - len][1 - pos] = d + c[u];
               st.insert({{dis[u][s[u].length() - len][1 - pos] , u} , {s[u].length() - len , 1 - pos}});
            }
         }
      }
   }
}

bool pal(string t){
   int n = t.length();
   for(int i = 0 ; i < n ; i++)
      if(t[i] != t[n - 1 - i])
         return false;
   return true;
}

int32_t main(){
   ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
   int n;
   cin >> n;
   for(int i = 0 ; i < n ; i++){
      cin >> s[i] >> c[i];
      if(pal(s[i])){
         ans = min(ans , 1ll * c[i]);
         n--;
         i--;
      }
   }
   if(n == 0)
      return cout << ans , 0;
   for(int i = 0 ; i < n ; i++){
      string ask = "";
      for(int j = s[i].length() - 1 ; j >= 0 ; j--){
         ask = s[i][j] + ask;
         p[i][s[i].length() - j][0] = pal(ask);
      }
      ask = "";
      for(int j = 0 ; j < s[i].length() ; j++){
         ask += s[i][j];
         p[i][j + 1][1] = pal(ask);
      }
   }
   for(int i = 0 ; i < n ; i++){
      for(int j = 1 ; j <= s[i].length() ; j++){
         for(int k = 0 ; k < n ; k++){
            bool bad = false;
            for(int p = 0 ; p < min(j , int(s[k].length())) ; p++){
               if(s[i][s[i].length() - j + p] != s[k][s[k].length() - 1 - p])
                  bad = true;
            }
            if(!bad)
               g[i][j][0].push_back(k);

            bad = false;
            for(int p = 0 ; p < min(j , int(s[k].length())) ; p++){
               if(s[i][j - 1 - p] != s[k][p])
                  bad = true;
            }
            if(!bad)
               g[i][j][1].push_back(k);
         }
      }
   }
   for(int i = 0 ; i < N ; i++)
      for(int j = 0 ; j < N ; j++)
         for(int k = 0 ; k < 2 ; k++)
            dis[i][j][k] = 1ll * mod * mod;
   for(int i = 0 ; i < n ; i++){
      st.insert({{c[i] , i} , {s[i].length() , 0}});
      dis[i][s[i].length()][0] = c[i];
   }
   dijekstra();
   if(ans == 1ll * mod * mod)
      ans = -1;
   cout << ans;
   return 0;
}


Measured memory_bytes: 3860