class String
  def mirror?
    (1 .. size / 2).all?{|i| self[i - 1] == self[size - i]}
  end
end
N = gets.to_i
S = (1 .. N).map do
  s,c = gets.split
  [s.chomp, c.to_i]
end

used_suffix = {}
used_prefix = {}

q = []
S.each do |s,c|
  q << [s, "", c]
  q << ["", s, c]
end
until q.empty?
  status = q.min_by(&:last)
  q.delete(status)
  l,r,t = *status
  lr = l + r
  if lr.size > 0 && lr.mirror?
    puts t
    exit
  end
  
  if l.size > r.size
    suffix = l[r.size, l.size - r.size].reverse
    next if used_suffix[suffix]
    used_suffix[suffix] = true
    S.select{|s,c| s.size >= suffix.size ? s.end_with?(suffix) : suffix.end_with?(s) }
      .each{|s,c| q.push([l, s + r, t + c]) }
  elsif l.size < r.size
    prefix = r[0, r.size - l.size].reverse
    next if used_prefix[prefix]
    used_prefix[prefix] = true
    S.select{|s,c| s.size >= prefix.size ? s.start_with?(prefix) : prefix.start_with?(s) }
      .each{|s,c| q.push([l + s, r, t + c]) }
  else
    raise "unreachable"
  end
end
puts -1
