h, w = gets.split.map(&:to_i)
ch, cw = gets.split.map(&:to_i)
dh, dw = gets.split.map(&:to_i)
grid = []
h.times do
  grid << gets.chomp.split("")
end
memo = Array.new(h) { Array.new(w) }
current_group = 0
h.times do |row|
  current_grid = grid[row]
  current_memo = memo[row]
  w.times do |col|
    next if current_memo[col]
    next if current_grid[col] == "#"
    queue = [[row, col]]
    until queue.empty?
      current_row, current_col = queue.shift
      memo[current_row][current_col] = current_group
      if current_row <= h - 2 && grid[current_row + 1][current_col] == "." && memo[current_row + 1][current_col].nil?
        queue << [current_row + 1, current_col]
      end
      if current_row > 0 && grid[current_row - 1][current_col] == "." && memo[current_row - 1][current_col].nil?
        queue << [current_row - 1, current_col]
      end
      if current_col <= w - 2 && grid[current_row][current_col + 1] == "." && memo[current_row][current_col + 1].nil?
        queue << [current_row, current_col + 1]
      end
      if current_col > 0 && grid[current_row][current_col - 1] == "." && memo[current_row][current_col - 1].nil?
        queue << [current_row, current_col - 1]
      end
    end
    current_group += 1
  end
end

edges = Array.new(current_group) { Hash.new }
# up = 0
# down = [2, w - 1].min
# current_rows = []
# (up..down)
memo.each_with_index do |row, r_index|
  current_rows = []
  current_rows << memo[r_index - 2] if r_index > 1
  current_rows << memo[r_index - 1] if r_index > 0
  current_rows << memo[r_index] if r_index
  current_rows << memo[r_index + 1] if r_index <= h - 2
  current_rows << memo[r_index + 2] if r_index <= h - 3
  row.each_with_index do |col, c_index|
    next if col.nil?
    # current_rows = []
    # current_rows << memo[r_index - 2] if r_index > 1
    # current_rows << memo[r_index - 1] if r_index > 0
    # current_rows << memo[r_index] if r_index
    # current_rows << memo[r_index + 1] if r_index <= h - 2
    # current_rows << memo[r_index + 2] if r_index <= h - 3
    current_rows.each do |current_row|
      left = c_index - 2
      left = 0 if left < 0
      right = c_index + 2
      right = w - 1 if right > w - 1
      (left..right).each do |i|
        next unless cur = current_row[i]
        next if cur == col
        edges[col][cur] = true
      end
    end
  end
end

start = memo[ch - 1][cw - 1]
goal = memo[dh - 1][dw - 1]
if start == goal
  puts 0
  exit
end
# current, from, cost
queue = [[start, start, 0]]

ans = -1
visited = Array.new(current_group, false)
until queue.empty?
  cur, from, cost = queue.shift
  visited[cur] = true
  edges[cur].keys.each do |key|
    if key == goal
      ans = cost + 1
      break
    end
    next if key == from || visited[key]
    queue << [key, cur, cost + 1]
  end
  break if ans != -1
end
puts ans
