#include <bits/stdc++.h>

#if DEBUG
template <typename Iter>
void debug_iter(Iter begin, Iter end);
template <typename T>
void debug(const T& value) {
  std::cerr << value;
}
template <typename T, size_t N>
void debug(const std::array<T, N>& a) {
  debug_iter(a.begin(), a.end());
}
template <typename T>
void debug(const std::vector<T>& v) {
  debug_iter(v.begin(), v.end());
}
template <typename T>
void debug(const std::set<T>& s) {
  debug_iter(s.begin(), s.end());
}
template <typename T, typename U>
void debug(const std::map<T, U>& m) {
  debug_iter(m.begin(), m.end());
}
template <typename T, typename U>
void debug(const std::pair<T, U>& p) {
  std::cerr << "{";
  debug(p.first);
  std::cerr << ", ";
  debug(p.second);
  std::cerr << "}";
}
template <typename T, typename... Ts>
void debug(const T& value, const Ts&... args) {
  debug(value);
  std::cerr << ", ";
  debug(args...);
}
template <typename Iter>
void debug_iter(Iter begin, Iter end) {
  std::cerr << "{";
  for (Iter it = begin; it != end; ++it) {
    if (it != begin) std::cerr << ", ";
    debug(*it);
  }
  std::cerr << "}";
}
#define dbg(...)                        \
  do {                                  \
    cerr << #__VA_ARGS__ << ": ";       \
    debug(__VA_ARGS__);                 \
    cerr << " (L" << __LINE__ << ")\n"; \
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define rd(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);

template <typename T>
void write_to_cout(const T& value) {
  std::cout << value << '\n';
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define wt(...) write_to_cout(__VA_ARGS__);

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
  for (T& vi : v) is >> vi;
  return is;
}

template <typename T>
bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T>
bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

#include <boost/hana/functional/fix.hpp>
auto Fix = [](auto&& f) { return boost::hana::fix(f); };

using ll = long long;

template <typename T>
using V = std::vector<T>;
template <typename T>
using VV = V<V<T>>;

using namespace std;

using u64 = unsigned long long;

struct S {
  string l, r;
  bool operator<(const S& s) const {
    if (l != s.l) return l < s.l;
    return r < s.r;
  }
};

int main() {
  rd(int, n);
  V<string> s(n);
  V<ll> c(n);
  rep(i, n) cin >> s[i] >> c[i];
  priority_queue<pair<ll, S>> q;
  q.push({0, {"", ""}});
  map<S, ll> seen;
  while (!q.empty()) {
    auto [cost, h] = q.top();
    cost = -cost;
    q.pop();
    dbg(cost, h.l, h.r);
    if (cost != 0 && h.l.empty() && h.r.empty()) {
      wt(cost);
      return 0;
    }
    auto prefixMatch = [](const string& a, const string& b) {
      rep(i, min(a.size(), b.size())) if (a[i] != b[i]) return false;
      return true;
    };
    rep(i, n) {
      S t(h);
      if (t.l.empty() && t.r.empty()) {
        t.l = s[i];
      } else if (t.l.empty()) {
        if (!prefixMatch(t.r, s[i])) continue;
        t.l = s[i];
      } else {
        assert(t.r.empty());
        string rs = s[i];
        reverse(all(rs));
        if (!prefixMatch(t.l, rs)) continue;
        t.r = rs;
      }
      int pl = min(t.l.size(), t.r.size());
      t.l = t.l.substr(pl);
      t.r = t.r.substr(pl);
      assert(t.l.empty() || t.r.empty());
      auto palin = [](const string& s) {
        rep(i, s.size() / 2) if (s[i] != s[s.size() - 1 - i]) return false;
        return true;
      };
      if (palin(t.l)) t.l = "";
      if (palin(t.r)) t.r = "";
      bool empty = t.l.empty() && t.r.empty();
      auto it = seen.find(t);
      ll nc = cost + c[i];
      if (!empty && it != seen.end() && it->second <= nc) continue;
      seen[t] = nc;
      q.push({-(cost + c[i]), t});
    }
  }
  wt(-1);
#undef int
}


Measured memory_bytes: 3696