x,y,z = gets.split.map(&:to_i) a = Hash.new a["A"] = x a["B"] = y a["C"] = z s = [] t = [] s[0],t[0] = gets.split s[1],t[1] = gets.split t[0] = t[0].to_i t[1] = t[1].to_i ans = 10**10 u = [t[0]-1,a[s[0]]-t[0]] v = [t[1]-1,a[s[1]]-t[1]] ans = (t[0]-t[1]).abs if (s[0] == s[1]) 2.times do |i| 2.times do |j| r = u[i]+v[j]+1 r += [x,y,z].min if i != j ans = [ans,r].min end end puts ans