h, w = gets.split.map(&:to_i)
ch, cw = gets.split.map(&:to_i)
dh, dw = gets.split.map(&:to_i)
s = h.times.map {gets}

d = Array.new(h) {Array.new(w)}
now = [[ch-1, cw-1]]
i = 0

warp = (-2..2).to_a.product((-2..2).to_a) - [[0,0], [1,0], [-1,0], [0,1], [0,-1]]

while !now.empty?
    re = []
    while (x, y = now.shift)
        d[x][y] = i
        re << [x, y]
        now << [x-1, y] if x > 0 && s[x-1][y] == ?. && !d[x-1][y]
        now << [x+1, y] if x < h-1 && s[x+1][y] == ?. && !d[x+1][y]
        now << [x, y-1] if y > 0 && s[x][y-1] == ?. && !d[x][y-1]
        now << [x, y+1] if y < w-1 && s[x][y+1] == ?. && !d[x][y+1]
    end

    re.each do |x,y|
        warp.each do |i,j|
            xx = x + i
            yy = y + j
            now << [xx,yy] if xx.between?(0,h-1) && yy.between?(0,w-1) && s[xx][yy] == ?. && !d[xx][yy]
        end
    end

    re.clear
    i += 1
end

p d[dh-1][dw-1] ? d[dh-1][dw-1] : -1