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

init = 1 << 30
d = Array.new(h+2) {Array.new(w+2, 0)}
h.times do |i|
    gets.each_char.with_index do |c,j|
        d[i][j] = init if c == ?.
    end
end

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

t = [[ch-1, cw-1]]
(0..).each do |k|
    break if t.empty? || d[dh-1][dw-1] < init
    u = []

    while (x, y = t.pop)
        next if d[x][y] <= k
        d[x][y] = k

        move.each do |i,j|
            t << [x+i, y+j] if d[x+i][y+j] > i
        end

        warp.each do |i,j|
            u << [x+i, y+j] if d[x+i][y+j] > i
        end
    end

    t = u
end

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