lines = []
while line = gets
    lines << line.chomp.split(' ')
end

H = lines[0][0].to_i
W = lines[0][1].to_i
Inf = 10 ** 7

start_h = lines[1][0].to_i
start_w = lines[1][1].to_i
goal_h = lines[2][0].to_i
goal_w = lines[2][1].to_i

min_magic = []
for i in 1..H do
  min_magic[i-1] = []
  for j in 1..W do
    min_magic[i-1][j-1] = Inf
  end
end

visited = []
for i in 1..H do
  visited[i-1] = []
  for j in 1..W do
    visited[i-1][j-1] = false
  end
end

target = [[start_h, start_w]]
current_magic = 0
while target != [] do
  sub_target = []
  while target != [] do
    i = target.pop
    if i[0] > 0 and i[1] > 0 and i[0] <= H and i[1] <= W and lines[i[0]+2][0][i[1]-1] == "." and visited[i[0]-1][i[1]-1] == false then
      sub_target.push(i)
      visited[i[0]-1][i[1]-1]=true
      min_magic[i[0]-1][i[1]-1] = current_magic
      target.push([i[0]+1,i[1]], [i[0]-1,i[1]], [i[0],i[1]+1], [i[0],i[1]-1])
    end
  end
  current_magic += 1
  sub_target.each do |i|
    [-2,-1,0,1,2].each do |j|
      [-2,-1,0,1,2].each do |k|
        ni = [i[0]+j,i[1]+k]
        if ni[0] > 0 and ni[1] > 0 and ni[0] <= H and ni[1] <= W and lines[ni[0]+2][0][ni[1]-1] == "." and visited[ni[0]-1][ni[1]-1] == false then
          target.push(ni)
        end
      end
    end
  end
end
  
ans = min_magic[goal_h-1][goal_w-1]
if ans == Inf then
  puts -1
else
  puts ans
end