DY = [-1, 0, 1, 0]
DX = [0, 1, 0, -1]

H, W = gets.split.map(&:to_i)
CH, CW = gets.split.map(&:to_i)
DH, DW = gets.split.map(&:to_i)
MAP = H.times.map { gets.chomp.chars }

visited = Array.new(H) { Array.new(W, false) }
queue = []
queue << [CH - 1, CW - 1, 0]

until queue.empty?
  y, x, cost = queue.shift

  next if visited[y][x]
  visited[y][x] = true

  if DH - 1 == y && DW - 1 == x
    puts cost
    exit
  end

  4.times do |i|
    ny = y + DY[i]
    nx = x + DX[i]

    next if ny < 0 || nx < 0 || H <= ny || W <= nx
    next if visited[ny][nx]
    next if MAP[ny][nx] == '#'

    queue.unshift([ny, nx, cost])
  end

  -2.upto(2) do |dy|
    -2.upto(2) do |dx|
      ny = y + dy
      nx = x + dx

      next if ny < 0 || nx < 0 || H <= ny || W <= nx
      next if visited[ny][nx]
      next if MAP[ny][nx] == '#'

      queue.push([ny, nx, cost + 1])
    end
  end
end

puts -1
