# { :pre :post :cost }

def run(n,dic)
  data = [{:pre => "", :post => "", :cost=>0}]
  data = dic.map { | s,c |
    {:pre => s, :post => "", :cost => c}
  }
  q = 0
  while true
    q = q + 1
    if q > 10 then
      break
    end
    data = data.sort { |x,y| x[:cost] <=> y[:cost]}
#    p data
    if data.size == 0 then
      puts -1
      return
    elsif ! (data[0][:pre].start_with?(data[0][:post].reverse) || data[0][:post].reverse.start_with?(data[0][:pre])) then
#      p "A"
      data.shift
    elsif (data[0][:pre]+data[0][:post])==(data[0][:pre]+data[0][:post]).reverse && data[0][:cost] > 0 then
#      p "B"
      puts data[0][:cost]
      return
    elsif data[0][:cost] > 1000000000 then
      puts -1
      return
    elsif data.size == 0 then
      puts -1
      return
    else
      data1 = data.shift
 
      dic.each { | s,c |
        if data1[:post] != "" then
          data.push({ :pre => data1[:pre] + s,
                      :post => data1[:post],
                      :cost => data1[:cost] + c})
        end
        data.push({ :pre => data1[:pre],
                    :post => s + data1[:post],
                    :cost => data1[:cost] + c})
      }
    end
  end
end

while
  n = gets
  if ! n then
    exit
  end
  n = n.chomp.to_i
  dic = []
  n.times.each { | i |
    s,c = gets.chomp.split(' ')
    c = c.to_i
    dic.push [s,c]
  }

  run(n,dic)
  
end

