#include <bits/stdc++.h>

using namespace std;

const int N = 111;
const int M = 22;
const long long inf = 1e18;

vector<tuple<int, int, int>> g[N][M];

pair<int, int> neutral = make_pair(101, 21);

string s[N];
int cost[N];
int n;

bool is_palindrome(const string &s) {
  int m = (int) s.size();
  for (int i = 0; i + i < m; i++) {
    if (s[i] != s[m - 1 - i]) {
      return false;
    }
  }
  return true;
}

void make() {
  for (int i1 = 0; i1 < 2 * n; i1++) {
    int m1 = (int) s[i1].size();
    for (int j1 = 0; j1 < m1; j1++) {
      if (is_palindrome(s[i1].substr(j1))) {
        g[neutral.first][neutral.second].emplace_back(i1, j1, 0);
      }
      for (int i2 = (i1 < n ? n : 0); i2 < (i1 < n ? 2 * n : n); i2++) {
        int m2 = (int) s[i2].size();
        int p1 = j1;
        int p2 = 0;
        while (p1 < m1 && p2 < m2 && s[i1][p1] == s[i2][p2]) {
          ++p1;
          ++p2;
        }
        if (p1 != m1 && p2 != m2) continue;
        if (p2 != m2) {
          g[i2][p2].emplace_back(i1, j1, cost[i2]);
        } else if (p1 != m1) {
          g[i1][p1].emplace_back(i1, j1, cost[i2]);
        } else {
          g[neutral.first][neutral.second].emplace_back(i1, j1, cost[i2]);
        }
      }
    }
  }
}

long long dist[N][M];

void dijk(pair<int, int> src) {
  memset(dist, -1, sizeof dist);
  dist[src.first][src.second] = 0;
  set<pair<long long, pair<int, int>>> st;
  st.insert({0, src});
  while (!st.empty()) {
    auto v = st.begin()->second;
    st.erase(st.begin());
    int i1 = v.first;
    int j1 = v.second;
    for (auto to : g[i1][j1]) {
      int i2 = get<0>(to);
      int j2 = get<1>(to);
      int cost = get<2>(to);
      if (dist[i2][j2] == -1 || dist[i1][j1] + cost < dist[i2][j2]) {
        if (dist[i2][j2] != -1) {
          st.erase({dist[i2][j2], {i2, j2}});
        }
        dist[i2][j2] = dist[i1][j1] + cost;
        st.insert({dist[i2][j2], {i2, j2}});
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i] >> cost[i];
    s[n + i] = s[i];
    cost[n + i] = cost[i];
    reverse(s[n + i].begin(), s[n + i].end());
  }
  make();
  dijk(neutral);
  long long res = inf;
  for (int i = 0; i < n; i++) {
    if (dist[i][0] != -1) {
      res = min(res, dist[i][0] + cost[i]);
    }
  }
  cout << (res == inf ? -1 : res) << '\n';
  return 0;
}
