# frozen_string_literal: true in_h, in_w = gets.chomp.split.map(&:to_i) in_a, in_b = gets.chomp.split.map(&:to_i) in_r1, in_c1, in_r2, in_c2 = gets.chomp.split.map(&:to_i) in_p, in_q = gets.chomp.split.map(&:to_i) def distance_between(a, b, c, d) (a - c).abs + (b - d).abs end min = 10**10 (in_r1..in_r2).each do |r| (in_c1..in_c2).each do |c| min = [ distance_between(in_a, in_b, r, c) + distance_between(r, c, in_p, in_q), min ].min end end puts min + distance_between(in_p, in_q, in_a, in_b)