require 'set'; require 'prime'; require 'pp'
INF=Float::INFINITY; MOD=10**9+7

class PQ
  attr_reader :elements
  def initialize(array = nil, &block)
    @elements = [nil]
    array.each{|e| push(e)} if array
    @compare_func = block
  end

  def push(element)
    @elements << element
    bubble_up(@elements.size - 1)
  end

  def pop
    swap(1, @elements.size - 1)
    max = @elements.pop
    bubble_down(1)
    max
  end

  def size() @elements.size - 1 end
  def top() @elements[1] end
  def empty?() @elements.size == 1 end

  private

  def compare(a, b)
    @compare_func.call(a, b)
  end

  def bubble_up(idx)
    parent_idx = idx / 2
    return if idx <= 1
    return if compare(@elements[parent_idx], @elements[idx])

    swap(idx, parent_idx)
    bubble_up(parent_idx)
  end

  def bubble_down(idx)
    child_idx = (idx * 2)
    return if child_idx > @elements.size - 1

    not_the_last_element = child_idx < @elements.size - 1
    left_element = @elements[child_idx]
    right_element = @elements[child_idx + 1]
    child_idx += 1 if not_the_last_element && compare(right_element, left_element)

    return if compare(@elements[idx], @elements[child_idx])

    swap(idx, child_idx)
    bubble_down(child_idx)
  end

  def swap(source, target)
    @elements[source], @elements[target] = @elements[target], @elements[source]
  end
end

h,w = gets.chomp.split.map(&:to_i)
ch, cw = gets.chomp.split.map(&:to_i).map{|e| e-1}
dh, dw = gets.chomp.split.map(&:to_i).map{|e| e-1}
grid = []
h.times do
  grid.push gets.chomp.chars
end

ans = 0

q = PQ.new do |a,b|
  a[0] < b[0]
end
q.push [0, ch, cw]
grid[ch][cw] = 0

dy = [0, 1, 0, -1]; dx = [1, 0, -1, 0]

until q.empty?
  count, y, x = q.pop
  ans = [count, ans].max
  gridyx = grid[y][x]
  # p ['main', count, [y, x], gridyx]
  next if gridyx != '#' && gridyx != '.' && gridyx < count
  4.times do |i|
    ny = y + dy[i]; nx = x + dx[i]
    if 0 <= ny && ny < h && 0 <= nx && nx < w && grid[ny][nx] != '#'
      if grid[ny][nx] == '.'
        grid[ny][nx] = count
        q.push [count, ny, nx]
      elsif grid[ny][nx] > count
        grid[ny][nx] = count
        q.push [count, ny, nx]
      end
    end
  end

  (-2).upto(2) do |i|
    (-2).upto(2) do |j|
      ny = y + i
      nx = x + j
      if 0 <= ny && ny < h && 0 <= nx && nx < w && grid[ny][nx] != '#'
        if grid[ny][nx] == '.'
          grid[ny][nx] = count + 1
          q.push [count + 1, ny, nx]
        elsif grid[ny][nx] > count + 1
          grid[ny][nx] = count + 1
          q.push [count + 1, ny, nx]
        end
      end
    end
  end
end

# grid.each{|l| p l }
if grid[dh][dw] == '.' || grid[dh][dw] == '#'
  puts -1
else
  puts grid[dh][dw]
end
