N = gets.to_i
SCs = N.times.map{s, c = gets.split; [s, c.to_i]}

class String
  def pal?
    size > 0 &&(1 .. size/2).all?{|i|self[i - 1] == self[size - i]}
  end
end
class Word
  include Comparable
  attr_accessor :l, :r, :c

  def initialize(l, r, c)
    @l, @r, @c = l, r, c
  end

  def <=>(other)
    other.c <=> @c
  end
end

class PriorityQueue
  def initialize
    @q = [nil]
  end

  def bubble_up(i)
    pi = i / 2
    return if i <= 1 || @q[pi] >= @q[i]
    @q[i], @q[pi] = @q[pi], @q[i]
    bubble_up(pi)
  end

  def add(e)
    @q << e
    bubble_up(@q.size - 1)
  end

  def bubble_down(i)
    ci = i * 2
    return if ci > @q.size - 1
    ci += 1 if ci != @q.size - 1 && @q[ci] < @q[ci + 1]
    return if @q[i] >= @q[ci]
    @q[i], @q[ci] = @q[ci], @q[i]
    bubble_down(ci)
  end

  def pop
    @q[1], @q[-1] = @q[-1], @q[1]
    ret = @q.pop
    bubble_down(1)
    ret
  end

  def empty?
    @q.size == 1
  end
end

hs, hp = {}, {}
q = PriorityQueue.new
SCs.each{|s, c| q.add(Word.new(s, "", c)); q.add(Word.new("", s, c))}
while !q.empty?
  w = q.pop
  l, r, d = w.l, w.r, w.c
  ls, rs = l.length, r.length
  lr = l + r
  (puts d; exit) if lr.length > 0 && lr.pal?
  if ls > rs
    sf = l[rs, ls - rs].reverse
    next if hs[sf]
    hs[sf] = true
    SCs.select{|s, _| x, y = [s, sf].sort_by(&:length); y.end_with?(x)}.each{|s, c| q.add(Word.new(l, s + r, c + d))}
  else
    pf = r[0, rs - ls].reverse
    next if hs[pf]
    hp[pf] = true
    SCs.select{|s, _| x, y = [s, pf].sort_by(&:length); y.start_with?(x)}.each{|s, c| q.add(Word.new(l + s, r, c + d))}
  end
end
puts -1