class Keiro def initialize(v, s, t, y, m) @num = v @s_ms = s @e_ms = t @costs = y @times = m end def min_time(s_machi, e_machi, coin) list = [] @num.times do |i| if s_machi == @s_ms[i] nokori = coin - @costs[i] if nokori >= 0 if e_machi == @e_ms[i] list << @times[i] else kt = min_time(@e_ms[i], e_machi, nokori) if kt list << kt + @times[i] end end end end end if list.empty? return nil else return list.min end end end n = gets.to_i c = gets.to_i v = gets.to_i s = gets.split.map(&:to_i) t = gets.split.map(&:to_i) y = gets.split.map(&:to_i) m = gets.split.map(&:to_i) keiro = Keiro.new(v, s, t, y, m) mt = keiro.min_time(1, n, c) || -1 puts mt