@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('') }
@dist = Array.new(@a).map{Array.new(@b, 10**10)}
@deque = []
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

@deque << [@s_x-1, @s_y-1, 0]
@dist[@s_x-1][@s_y-1] = 0
while @deque.size > 0 
  x, y= @deque.shift
  d = @dist[x][y]
  for dir in 0..3 do
    nx = x + @dx[dir]
    ny = y + @dy[dir]

    
    next if (ny < 0 || nx >= @a || nx < 0 || ny >= @b) 
    next if (@data[nx][ny] == '#')
    next if (@dist[nx][ny] <= d)
    @dist[nx][ny] = d
    @deque.unshift [nx, ny, d]
  end
  @w_p.each do |w|
    w_x = w[0]
    w_y = w[1]
    nx = x + w_x
    ny = y + w_y

    next if (ny < 0 || nx >= @a || nx < 0 || ny >= @b) 
    next if (@data[nx][ny] == '#')
    next if (@dist[nx][ny] <= d+1)
    @dist[nx][ny] = d+1
    next if @deque.include?([nx, ny, d])
    @deque << [nx, ny, d+1] 
  end
end

ans = @dist[@g_x-1][@g_y-1]
if ans == 10**10
  puts -1
else
  puts ans
end
    
  