#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <complex>
#include <vector>
#include <limits>
#include <iomanip>
#include <cassert>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define debug(x) cout << #x << " = " << (x) << endl;
#define rep(i, n) for(int i = 0;i < n;i++)
#define repr(i, n) for(int i = n-1;i >= 0;i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second

const long double pi = 3.141592653589793;
const int mod = 1e9 + 7;

string A[55];int C[55];
int N;
ll d[25025];

int x[55], y[55], z[55], cost[55], in;
int a, b, c;
void n2s(int v) {
  c = v%21;
  v /= 21;
  b = v%21;
  v /= 21;
  a = v;
}

void reachable_from(int v) {
  n2s(v);
  in=0;
  for(int i = 1;i <= N;i++) {
    if(c == 0) {
      x[in]=i;
      y[in]=!b;
      z[in]=A[i].size();
      cost[in]=C[i];
      in++;
      continue;
    }
    if(b == 1) {
      int k = 0, j = c-1;
      while(k < A[i].size() && j >= 0) {
        if(A[i][k] != A[a][j]) break;
        k++;j--;
      }
      if(k == A[i].size() && j == -1) {
        x[in]=i;
        y[in]=!b;
        z[in]=0;
        cost[in]=C[i];
        in++;
        continue;
      }
      if(k == A[i].size()) {
        x[in]=a;
        y[in]=b;
        z[in]=j+1;
        cost[in]=C[i];
        in++;
        continue;
      }
      if(j == -1) {
        x[in]=i;
        y[in]=!b;
        z[in]=A[i].size()-c;
        cost[in]=C[i];
        in++;
      }
    } else {
      int k = A[i].size()-1, j = A[a].size()-c;
      while(k >= 0 && j < A[a].size()) {
        if(A[i][k] != A[a][j]) break;
        k--;j++;
      }
      if(k == -1 && j == A[a].size()) {
        x[in]=i;
        y[in]=!b;
        z[in]=0;
        cost[in]=C[i];
        in++;
        continue;
      }
      if(k == -1) {
        x[in]=a;
        y[in]=b;
        z[in]=A[a].size()-j;
        cost[in]=C[i];
        in++;
        continue;
      }
      if(j == A[a].size()) {
        x[in]=i;
        y[in]=!b;
        z[in]=k+1;
        cost[in]=C[i];
        in++;
      }
    }
  }
}

bool terminal(int v) {
  n2s(v);
  string s = A[a];
  if(!b) reverse(s.begin(),s.end());
  int i = 0, j = c-1;
  while(i < j) {
    if(s[i] != s[j]) return false;
    i++;j--;
  }
  return true;
}


int main() {

 // freopen("input.in","r",stdin);
 // freopen("output.out","w",stdout);

 ios_base::sync_with_stdio(false);
 cin.tie(nullptr);
 cin>>N;
 for(int i = 1;i <= N;i++){
   cin >> A[i] >> C[i];
 }
 ll ans = 1e16;
 rep(i, 25025) d[i] = 1e16;
 d[0] = 0;
 set<pair<ll, int>> q;
 q.insert({0, 0});
 while (!q.empty()) {
  int v = q.begin()->second;
  q.erase(q.begin());

  // check terminal step
  if(v != 0 && terminal(v)) {
    ans = min(ans, d[v]);
    continue;
  }

  // states reachable from v
  reachable_from(v);
  rep(i, in) {
    int u = x[i]*441 + y[i]*21 + z[i];
    int w = cost[i];
    if(d[v] + w  < d[u]) {
      q.erase({d[u], u});
      d[u] = d[v] + w;
      q.insert({d[u], u});
    }
  }

 }

 if(ans == 1e16) cout << -1 << endl;
 else cout << ans << endl;

 return 0;
}


Measured memory_bytes: 3788