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, 0]]
warpq = []

while(fq = fillq.shift) do
    h, w, d, wd = *fq
    next if S[h][w].class == Integer
    if h == Dh && w == Dw
        puts d
        exit
    end
    S[h][w] = d
    blocked = false
    [
        [h-1,w],
        [h+1,w],
        [h,w-1],
        [h,w+1]
    ].each do |_h, _w|
        t = S[_h][_w]
        if t == '.'
            fillq.unshift([_h,_w,d])
        elsif t == '#'
            blocked = true
        end
    end
    [
        [h-2,w-2],
        [h-2,w-1],
        [h-2,w],
        [h-2,w+1],
        [h-2,w+2],
        [h-1,w-2],
        [h-1,w-1],
        [h-1,w+1],
        [h-1,w+2],
        [h,w-2],
        [h,w+2],
        [h+1,w-2],
        [h+1,w-1],
        [h+1,w+1],
        [h+1,w+2],
        [h+2,w-2],
        [h+2,w-1],
        [h+2,w],
        [h+2,w+1],
        [h+2,w+2]
    ].each do |_h,_w|
        next if _h < 1 || _w < 1 || _h > H || _w > W
        next unless S[_h][_w] == "."
        fillq.push([_h,_w,d+1])
    end
end

puts -1