H, W = gets.split.map(&:to_i)
Ch, Cw = gets.split.map(&:to_i)
Dh, Dw = gets.split.map(&:to_i)
S = Array.new H + 2
S[0] = Array.new(W+2, "#")
H.times do |i|
    S[i+1] = ["#", *gets.chomp.split(''), "#"]
end
S[H+1] = Array.new(W+2, "#")

res = 0

fillq = [[Ch, Cw, 0]]
warpq = []

loop do
    while(fq = fillq.shift) do
        h, w, d = *fq
        if h == Dh && w == Dw
            puts d
            exit
        end
        S[h][w] = d
        warpq.push([h,w,d])
        if S[h-1][w] == '.'
            fillq.push([h-1, w, d])
        end
        if S[h+1][w] == '.'
            fillq.push([h+1, w, d])
        end
        if S[h][w-1] == '.'
            fillq.push([h, w-1, d])
        end
        if S[h][w+1] == '.'
            fillq.push([h, w+1, d])
        end
    end

    wq = warpq.shift
    unless wq
        puts -1
        exit
    end
    h, w, d =  *wq
    -2.upto(2) do |dh|
        _h = h + dh
        next if _h < 1 || _h > H
        -2.upto(2) do |dw|
            _w = w + dw
            next if _w < 1 || _w > W
            next unless S[_h][_w] == '.'
            fillq.push([_h, _w, d+1])
        end
    end
end