def dfs(y, x)
  @seen[y][x] = 1
  
  for dir in 0..3 do
    ny = y + @dx[dir]
    nx = x + @dy[dir]
    
    next if (ny < 0 || ny >= @a || nx < 0 || nx >= @b) 
    next if (@data[ny][nx] == '#')
    next if (@seen[ny][nx] == 1)
    dfs(ny, nx)
  end
end

def warp(y,x)
    @seen[y][x] = 1
    @w_p.each do |w|
      ny = y + w[0]
      nx = x + w[1]
      next if (ny < 0 || ny >= @a || nx < 0 || nx >= @b) 
      next if (@data[ny][nx] == '#')
      next if (@seen[ny][nx] == 1)
      @w_seen[ny][nx] = [1 + @w_seen[y][x], @w_seen[ny][nx]].min
      warp(ny,nx)
    end
end

@a, @b = gets.split.map(&:to_i)
@s_x, @s_y = gets.split(" ").map(&:to_i)
@g_x, @g_y = gets.split(" ").map(&:to_i)
@data = @a.times.map { gets.strip.split('') }
@seen = Array.new(@a).map{Array.new(@b, 0)}
@w_seen = Array.new(@a).map{Array.new(@b, 1)}
cnt = 0

@dx = [1, 0, -1, 0]
@dy = [0, 1, 0, -1]
w = [-2, -1, 0, 1, 2]
@w_p = w.repeated_permutation(2).to_a

dfs(@s_y-1, @s_x-1)

if @seen[@g_y-1][@g_x-1] == 1
  puts cnt
  exit
end


warp(@s_y-1, @s_x-1)
  
if @seen[@g_y-1][@g_x-1] >= 1
  puts @w_seen[@g_y-1][@g_x-1]
else
  puts -1
end

  
  