H,W = gets.split.map &:to_i
C = gets.split.map{ _1.to_i - 1}
D = gets.split.map{ _1.to_i - 1}

S = H.times.map{ gets.chomp.chars }

warp_count = Array.new(H){[10**10]*W}
warp_count[C[0]][C[1]] = 0

query = [C]
move = [[-1,0],[1,0],[0,-1],[0,1]]
warp = 0
loop do
    warp_q = []
    while q = query.shift
        h,w = q
        move.each do |mx,my|
            y = h+my
            x = w+mx
            next if (y<0) ||(H<=y) || (x<0) || (W<=x)
            next if warp_count[y][x] <= warp
            if S[y][x] == "#" then
                warp_q << [h,w]
                next
            end
            warp_count[y][x] = warp
            query << [y,x]
        end
    end

    warp += 1
    warp_q.uniq.each do |h,w|
        (-2..2).each do |my|
            y = h + my
            next if (y<0)||(H<=y)
            (-2..2).each do |mx|
                x = w + mx
                next if (x<0)||(W<=x)
                next if warp_count[y][x] <= warp
                next if S[y][x] == "#"
                warp_count[y][x] = warp
                query << [y,x]
            end
        end
    end

    query.uniq!
    break if query.empty?
end

ans = warp_count[D[0]][D[1]]
p (ans==10**10) ? -1 : ans
