#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
#define endl '\n'


template <typename A, typename B>
string to_string(pair<A, B> p);
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
 
string to_string(const string& s) {
  return '"' + s + '"';
}
 
string to_string(const char c) {
  return string(1,c);
}

string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
 
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (int i = int(N)-1; i >= 0; --i) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
 
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 COMPETITIVE_PROGRAMMING_LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define dbg(...) if(0){}
#endif

 
constexpr ll INF = INT64_MAX/8;

bool IsPal(string& s) {
  int ns = s.length();
  for (int i = 0; i < ns/2; ++i) {
    if (s[i] != s[ns-1-i]) return false;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
 
  ll N;
  cin >> N;
  vector<pair<string,ll>> A(N);
  for (auto& _a : A) cin >> _a.first >> _a.second;
  sort(A.begin(), A.end());
  for (int i = 1; i < N; ++i) {
    if (A[i].first == A[i-1].first) {
      assert(A[i].second >= A[i-1].second);
      A[i].second = A[i-1].second;
    }
  }
  sort(A.begin(), A.end()); // TODO i don't think this is necessary
  A.resize(unique(A.begin(),A.end())-A.begin());
  N = A.size();

  ll ans = INF;
  vector<bool> is_pal(N);
  for (int i = 0; i < N; ++i) {
    string s = A[i].first;
    reverse(s.begin(), s.end());
    if (s == A[i].first) {
      is_pal[i] = true;
      ans = min(ans, A[i].second);
    }
  }
  {
    int j = 0;
    for (int i = 0; i < N; ++i) if (!is_pal[i]) A[j++] = A[i];
    A.resize(j);
    N = A.size();
  }

  map<pair<string,string>,ll> D;
  using tup = tuple<ll,string,string>;
  priority_queue<tup,vector<tup>,greater<tup>> pq;

  for (int i = 0; i < N; ++i) {
    if (A[i].second < ans) pq.push({A[i].second, A[i].first, ""});
    if (A[i].second < ans) pq.push({A[i].second, "", A[i].first});
  }
  // for (int i = 0; i < N; ++i) {
  //   if (A[i].second < ans) {
  //     string s = A[i].first;
  //     reverse(s.begin(), s.end());
  //     pq.push({A[i].second, "", s});
  //     D[{"",s}] = A[i].second;
  //   }
  // }
  


  while (pq.size()) {
    auto [d,prefix,suffix] = pq.top(); pq.pop();
    // dbg(prefix,suffix, d);
    if ((prefix.empty() && IsPal(suffix)) || (suffix.empty() && IsPal(prefix))) {
      ans = min(ans, d);
      break;
    }
    if (D.count({prefix,suffix}) && d > D[{prefix,suffix}]) continue;
    if (prefix.length()) {
      for (int is = 0; is < N; ++is) {
        auto& [s,c] = A[is];
        if (d+c >= ans) continue;
        int ns = s.length();
        int nn = min(ns, int(prefix.length()));
        bool good = true;
        int ij = 0;
        for (; ij < nn; ++ij) if (prefix[ij] != s[ns-1-ij]) break;
        if (ij != nn) continue;
        if (nn == ns) {
          string new_prefix = prefix.substr(ns);
          if (!D.count({new_prefix,""}) || d+c < D[{new_prefix,""}]) {
            D[{new_prefix,""}] = d+c;
            pq.push({d+c, new_prefix, ""});
          }
        } else {
          string new_suffix = s.substr(0, ns-nn);
          if (!D.count({"",new_suffix}) || d+c < D[{"",new_suffix}]) {
            D[{"",new_suffix}] = d+c;
            pq.push({d+c, "", new_suffix});
          }
        }
      }
    } else {
      for (int is = 0; is < N; ++is) {
        auto& [s,c] = A[is];
        if (d+c >= ans) continue;
        //int ns = s.length();
        int n_suf = suffix.length();
        
        int nn = min(int(s.length()), n_suf);
        bool good = true;
        int ij = 0;
        for (; ij < nn; ++ij) if (suffix[n_suf-1-ij] != s[ij]) break;
        if (ij != nn) continue;
        if (nn == n_suf) {
          string new_prefix = s.substr(n_suf);
          if (!D.count({new_prefix,""}) || d+c < D[{new_prefix,""}]) {
            D[{new_prefix,""}] = d+c;
            pq.push({d+c, new_prefix, ""});
          }
        } else {
          string new_suffix = suffix.substr(0, n_suf-nn);
          if (!D.count({"",new_suffix}) || d+c < D[{"",new_suffix}]) {
            D[{"",new_suffix}] = d+c;
            pq.push({d+c, "", new_suffix});
          }
        }
      }
    }
  }
  if (ans == INF) ans = -1;
  cout << ans << endl;

}


Measured memory_bytes: 3724