class PriorityQueue
  class Node
    attr_reader :value, :priority
    def initialize(value, priority)
      @value = value.clone
      @priority = priority
    end
  end

  def initialize
    @heap = [nil]
  end

  def size
    @heap.size - 1
  end

  def empty?
    size == 0
  end

  def enqueue(value, priority)
    @heap.push(Node.new(value, priority))
    up_heap
    self
  end

  def dequeue
    return nil if empty?
    return @heap.pop.value if size == 1
    ret = @heap[1].value
    @heap[1] = @heap.pop
    down_heap
    ret
  end

  def peek
    @heap[1].value.dup
  end

  private

  def up_heap
    pos = size
    while pos > 1
      parent = pos / 2
      break if @heap[pos].priority <= @heap[parent].priority
      tmp = @heap[pos]
      @heap[pos] = @heap[parent]
      @heap[parent] = tmp
      pos = parent
    end
  end

  def down_heap
    pos = 1
    loop{
      left = pos * 2 
      right = pos * 2 + 1
      break if left >= @heap.size
      if right >= @heap.size || @heap[left].priority >= @heap[right].priority
        break if @heap[pos].priority >= @heap[left].priority
        tmp = @heap[pos]
        @heap[pos] = @heap[left]
        @heap[left] = tmp
        pos = left
      else
        break if @heap[pos].priority >= @heap[right].priority
        tmp = @heap[pos]
        @heap[pos] = @heap[right]
        @heap[right] = tmp
        pos = right
      end
    }
  end
end

H,W = gets.split.map(&:to_i)
ch,cw = gets.split.map{|i|i.to_i-1}
dh,dw = gets.split.map{|i|i.to_i-1}
S = []
H.times{
	S << gets.chomp
}
closed = Array.new(H){Array.new(W){false}}
q = PriorityQueue.new
q.enqueue([ch, cw, 0], 0)
dist = -1
until q.empty?
	h,w,d = q.dequeue
	next if closed[h][w]
	next if S[h][w] == "#"
	closed[h][w] = true
	if h==dh && w==dw
		dist = d
		break
	end
	if h > 0
		q.enqueue([h-1,w,d],-d)
	end
	if h < H - 1
		q.enqueue([h+1,w,d],-d)
	end
	if w > 0
		q.enqueue([h,w-1,d],-d)
	end
	if w < W - 1
		q.enqueue([h,w+1,d],-d)
	end
	(h-2 .. h+2).each{|hh|
		next if hh < 0
		next if hh > H - 1
		(w-2 .. w+2).each{|ww|
			next if ww < 0
			next if ww > W - 1
			q.enqueue([hh,ww,d+1],-d-1)
		}
	}
end
puts dist
