h, w = gets.split.map(&:to_i)
c = gets.split.map(&:to_i)
d = gets.split.map(&:to_i)
s = Array.new(h) { gets.chomp.chars }
t = Array.new(h) { Array.new(w, 0)}
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]
wqu << [c[0] - 1, c[1] - 1]
t[c[0] - 1][c[1] - 1] = 1
while que.size > 0 || wqu.size > 0
  while que.size > 0
    y, x = que.shift
    cnt = t[y][x]
#    puts "y=#{y} x=#{x} cnt=#{cnt}"
    if y + 1 < h
      if s[y + 1][x] == '.' && t[y + 1][x] == 0
        t[y + 1][x] = cnt
        que << [y + 1, x]
      elsif s[y + 1][x] == '#'
        wqu << [y, x]
      end
    end
    if y - 1 >=0
      if s[y - 1][x] == '.' && t[y - 1][x] == 0
        t[y - 1][x] = cnt
        que << [y - 1, x]
      elsif s[y - 1][x] == '#'
        wqu << [y, x]
      end
    end
    if x + 1 < w
      if s[y][x + 1] == '.' && t[y][x + 1] == 0
        t[y][x + 1] = cnt
        que << [y, x + 1]
      elsif s[y][x + 1] == '#'
        wqu << [y, x]
      end
    end
    if x - 1 >= 0
      if s[y][x - 1] == '.' && t[y][x - 1] == 0
        t[y][x - 1] = cnt
        que << [y, x - 1]
      elsif s[y][x - 1] == '#'
        wqu << [y, x]
      end
    end
  end
  while wqu.size > 0
    y, x = wqu.shift
    cnt = t[y][x]
#    puts "y=#{y} x=#{x} cnt=#{cnt} wqu"
    u.each do |j, i|
      next if y + j < 0 || y + j >= h
      next if x + i < 0 || x + i >= w
      if s[y + j][x + i] == '.' && t[y + j][x + i] == 0
        t[y + j][x + i] = cnt + 1
        que << [y + j, x + i]
      end
    end
  end
end
puts t[d[0] - 1][d[1] - 1] - 1