/*  -*- coding: utf-8 -*-
 *
 * f.cc: F - Making Palindrome
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 50;
const int MAX_L = 20;
const int MAX_M = MAX_N * MAX_L * 2 + 2;
const long long LINF = 1LL << 62;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<pii> vpii;

/* global variables */

char ss[MAX_N][MAX_L + 4];
int cs[MAX_N], ls[MAX_N], ids[MAX_N][MAX_L + 1][2];
vpii nbrs[MAX_M];
ll ds[MAX_M];

/* subroutines */

bool fwdmatch(int l0, char s0[], int l1, char s1[]) {
  for (int i = 0, j = l1 - 1 - i; i < l0 && j >= 0; i++, j--)
    if (s0[i] != s1[j]) return false;
  return true;
}

bool revmatch(int l0, char s0[], int l1, char s1[]) {
  for (int i = 0, j = l0 - 1 - i; i < l1 && j >= 0; i++, j--)
    if (s0[j] != s1[i]) return false;
  return true;
}

bool is_palindrome(int l, char s[]) {
  for (int i = 0, j = l - 1; i < j; i++, j--)
    if (s[i] != s[j]) return false;
  return true;
}

/* main */

int main() {
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%s%d", ss[i], cs + i);
    ls[i] = strlen(ss[i]);
  }

  memset(ids, -1, sizeof(ids));

  int st = 0, gl = 1, m = 2;
  for (int i = 0; i < n; i++)
    for (int j = 1; j <= ls[i]; j++)
      for (int k = 0; k < 2; k++)
	ids[i][j][k] = m++;
  //printf("m=%d\n", m);

  for (int i = 0; i < n; i++) {
    if (is_palindrome(ls[i], ss[i])) {
      nbrs[st].push_back(pii(gl, cs[i]));
      continue;
    }

    nbrs[st].push_back(pii(ids[i][ls[i]][0], cs[i]));
    //nbrs[st].push_back(pii(ids[i][ls[i]][1], cs[i]));
      
    for (int j = 1; j <= ls[i]; j++) {
      int u0 = ids[i][j][0], u1 = ids[i][j][1];

      for (int i1 = 0; i1 < n; i1++) {
	if (fwdmatch(j, ss[i] + ls[i] - j, ls[i1], ss[i1])) {
	  //printf("fwd(%d, %d, %d, %d)\n", j, i, ls[i1], i1);
	  if (j == ls[i1])
	    nbrs[u0].push_back(pii(gl, cs[i1]));
	  else if(j < ls[i1]) {
	    int v0 = is_palindrome(ls[i1] - j, ss[i1]) ?
	      gl : ids[i1][ls[i1] - j][1];
	    nbrs[u0].push_back(pii(v0, cs[i1]));
	  }
	  else {
	    int v0 =
	      is_palindrome(j - ls[i1], ss[i] + (ls[i] - (j - ls[i1]))) ?
	      gl : ids[i][j - ls[i1]][0];
	    nbrs[u0].push_back(pii(v0, cs[i1]));
	  }
	}

	if (revmatch(j, ss[i], ls[i1], ss[i1])) {
	  //printf("rev(%d, %d, %d, %d)\n", j, i, ls[i1], i1);
	  if (j == ls[i1])
	    nbrs[u1].push_back(pii(gl, cs[i1]));
	  else if(j < ls[i1]) {
	    int v1 =
	      is_palindrome(ls[i1] - j, ss[i1] + j) ?
	      gl : ids[i1][ls[i1] - j][0];
	    nbrs[u1].push_back(pii(v1, cs[i1]));
	  }
	  else {
	    int v1 = is_palindrome(j - ls[i1], ss[i]) ?
	      gl : ids[i][j - ls[i1]][1];
	    nbrs[u1].push_back(pii(v1, cs[i1]));
	  }
	}
      }
    }
  }

  fill(ds, ds + m, LINF);
  ds[st] = 0;

  priority_queue<pli> q;
  q.push(pli(0, st));

  while (! q.empty()) {
    pli u = q.top(); q.pop();
    ll ud = -u.first;
    int ui = u.second;
    if (ds[ui] != ud) continue;

    vpii &nbru = nbrs[ui];
    for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
      int vi = vit->first;
      ll vd = ud + vit->second;
      if (ds[vi] > vd) {
	ds[vi] = vd;
	q.push(pli(-vd, vi));
      }
    }
  }

  printf("%lld\n", (ds[gl] < LINF) ? ds[gl] : -1LL);
  return 0;
}


Measured memory_bytes: 3276