$h,$w = gets.split.map &:to_i
$ch,$cw = gets.split.map &:to_i
$dh,$dw = gets.split.map &:to_i

$ss = []

$h.times{
	$ss << (gets.chomp.split(""))
}

$walked = []
$ss.each{ |s|
	temp = []
	s.each{ |s_|
		if s_ == "." then
			temp << false
		else
			temp << nil
		end
	}
	$walked << temp
}

$joint = Array.new($h).map{ Array.new($w) }

def joint_fill(_x, _y)
	start_x = [0, _x - 2].max
	end_x = [$w - 1, _x + 2].min
	start_y = [0, _y - 2].max
	end_y = [$h - 1, _y + 2].min
	
	(start_x .. end_x).each{ |x|
		(start_y .. end_y).each{ |y|
			if $ss[y][x] == "." then
				if $joint[y][x] == nil then
					$joint[y][x] = {}
				end
				$joint[y][x][$maps.size] = true
			end
		}
	}
end

def walk(x_, y_)
	start = [ [x_, y_] ]
	map = [ [x_, y_] ]

	$walked[y_][x_] = true
	joint_fill(x_, y_)
	
	while (start.size > 0)
		if start.include?([$cw - 1, $ch - 1]) then
			$start_map = $maps.size
		end
	
		if start.include?([$dw - 1, $dh - 1]) then
			$end_map = $maps.size
		end

		next_ = []
		
		start.each{ |x, y|
			if x + 1 < $w && $walked[y][x + 1] == false then
				next_ << [x + 1, y]
				map << [x + 1, y]
				$walked[y][x + 1] = true
				joint_fill(x + 1, y)
			end
			
			if x - 1 >= 0 && $walked[y][x - 1] == false then
				next_ << [x - 1, y]
				map << [x - 1, y]
				$walked[y][x - 1] = true
				joint_fill(x - 1, y)
			end
			
			if y + 1 < $h && $walked[y + 1][x] == false then
				next_ << [x, y + 1]
				map << [x, y + 1]
				$walked[y + 1][x] = true
				joint_fill(x, y + 1)
			end

			if y - 1 >= 0 && $walked[y - 1][x] == false then
				next_ << [x, y - 1]
				map << [x, y - 1]
				$walked[y - 1][x] = true
				joint_fill(x, y - 1)
			end
		}
		
		start = next_
	end
	
	map
end

$start_map
$end_map

$maps = []

(0...$h).each{ |h_|
	(0...$w).each{ |w_|
		if $walked[h_][w_] == false then
			$maps << walk(w_, h_)
		end
	}
}

joint2 = {}

$maps.each.with_index{ |map, i|
	map.each{ |x,y|
		if $joint[y][x] != nil && $joint[y][x].size > 1 then
			if joint2[i] == nil then
				joint2[i] = []
			end
			
			$joint[y][x].delete(i)

			joint2[i] += $joint[y][x].keys
		end
	}
}

joint3 = {}
joint2.each{ |k,v|
	joint3[k] = v.uniq
}

count = 0

if $start_map == $end_map then
	puts 0
	exit
end

st = [$start_map]

while st.size > 0 
	count+= 1

	next_ = []
	st.each{ |s|
		if joint3[s] != nil then
			if joint3[s].include?($end_map) then
				puts count
				exit
			else
				next_ += joint3[s]
			end
		end
	}
	
	st = next_.uniq
end

puts -1
