h, w = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)
d = gets.split.map(&:to_i)
s = []
s << [-1] * (w + 4)
s << [-1] * (w + 4)
h.times do
  s << [-1] * 2 + gets.chomp.chars.map { _1 == '#' ? -1 : 0 } + [-1] * 2
end
s << [-1] * (w + 4)
s << [-1] * (w + 4)
u = [[-2, -2], [-2, -1], [-2, 0], [-2, 1], [-2, 2],
    [-1, -2], [-1, -1], [-1, 1], [-1, 2],
    [0, -2], [0, 2],
    [1, -2], [1, -1], [1, 1], [1, 2],
    [2, -2], [2, -1], [2, 0], [2, 1], [2, 2]
  ]
que = []
wqu = []
que << [c[0] + 1, c[1] + 1]
s[c[0] + 1][c[1] + 1] = 1
while que.size > 0 || wqu.size > 0
  while que.size > 0
    y, x = que.shift
    cnt = s[y][x]
    if s[y + 1][x] == 0
      s[y + 1][x] = cnt
      que << [y + 1, x]
    elsif s[y + 1][x] == -1
      wqu << [y, x]
    end
    if s[y - 1][x] == 0
      s[y - 1][x] = cnt
      que << [y - 1, x]
    elsif s[y - 1][x] == -1
      wqu << [y, x]
    end
    if s[y][x + 1] == 0
      s[y][x + 1] = cnt
      que << [y, x + 1]
    elsif s[y][x + 1] == -1
      wqu << [y, x]
    end
    if s[y][x - 1] == 0
      s[y][x - 1] = cnt
      que << [y, x - 1]
    elsif s[y][x - 1] == -1
      wqu << [y, x]
    end
  end
  while wqu.size > 0
    y, x = wqu.shift
    cnt = s[y][x]
    u.each do |j, i|
      if s[y + j][x + i] == 0
        s[y + j][x + i] = cnt + 1
        que << [y + j, x + i]
      end
    end
  end
end
puts s[d[0] + 1][d[1] + 1] - 1