program making_palindrome
  implicit none
  integer(8), parameter :: inf = 1e18
  integer :: n, i, l
  character(20) :: s(50, 0:1) = ''
  integer(8) :: c(50) = 0, m(50, 21, 0:1) = 0, x = inf
  logical :: u(50, 21, 0:1) = .false.
  read(*,*) n
  do i = 1, n
    read(*,*) s(i, 0), c(i)
    l = len_trim(s(i, 0))
    s(i, 1)(1:l) = reverse(s(i, 0)(1:l))
  end do
  do i = 1, n
    x = min(x, solve(i, 1, 0) + c(i))
  end do
  write(*,'(i0)') merge(x, -1_8, x < inf)
contains
  function reverse(s) result(res)
    character(*), intent(in) :: s
    character(len_trim(s)) :: res
    integer :: n, i
    n = len_trim(s)
    do i = 1, n
      res(i:i) = s(n - i + 1:n - i + 1)
    end do
  end
  recursive integer(8) function solve(i, j, k) result(res)
    integer, intent(in) :: i, j, k
    integer :: l, r, t, l1, l2, h
    logical :: f
    res = m(i, j, k)
    if (u(i, j, k)) return
    u(i, j, k) = .true.
    m(i, j, k) = inf
    l = j
    r = len_trim(s(i, k))
    f = .true.
    do while (l < r)
      if (s(i, k)(l:l) /= s(i, k)(r:r)) then
        f = .false.
        exit
      end if
      l = l + 1
      r = r - 1
    end do
    if (f) then
      m(i, j, k) = 0
      res = 0
      return
    end if
    l1 = len_trim(s(i, k))
    h = xor(k, 1)
    do t = 1, n
      l = j
      r = 1
      l2 = len_trim(s(t, 1 - k))
      do while (l <= l1 .and. r <= l2)
        if (s(i, k)(l:l) /= s(t, h)(r:r)) exit
        l = l + 1
        r = r + 1
      end do
      if (l == l1 + 1) then
        m(i, j, k) = min(m(i, j, k), solve(t, r, h) + c(t))
      end if
      if (r == l2 + 1) then
        m(i, j, k) = min(m(i, j, k), solve(i, l, k) + c(t))
      end if
    end do
    res = m(i, j, k)
  end
end program making_palindrome