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

maze=[]
route=[]
h.times do
    r = gets.chomp.split("")
    maze.push(r)
    route.push(Array.new(r.length, "."))
end

que=[[ch-1, cw-1]]
maze[ch-1][cw-1]=0
route[ch-1][cw-1]=0
while que.length > 0
    y,x = que.shift
    c=maze[y][x].to_i + 1
    r=route[y][x].to_i
    warp=true
    if y<h-1 && (route[y+1][x].to_i>r || maze[y+1][x] == '.')
        maze[y+1][x]=c
        route[y+1][x]=r
        que.push([y+1,x])
        warp=false
    end
    if y>0 && (route[y-1][x].to_i>r || maze[y-1][x] == '.')
        maze[y-1][x]=c
        route[y-1][x]=r
        que.push([y-1,x])
        warp=false
    end
    if x<w-1 && (route[y][x+1].to_i>r || maze[y][x+1] == '.')
        maze[y][x+1]=c
        route[y][x+1]=r
        que.push([y,x+1])
        warp=false
    end
    if x>0 && (route[y][x-1].to_i>r || maze[y][x-1] == '.')
        maze[y][x-1]=c
        route[y][x-1]=r
        que.push([y,x-1])
        warp=false
    end

    if warp
        (-2..2).each do |dy|
            py = y+dy
            if py>=0 && py<h
                (-2..2).each do |dx|
                    px= x+dx
                    if px>=0 && px<w && maze[py][px] == "."
                        maze[py][px]=c
                        route[py][px]=r+1
                        que.push([py,px])
                    end
                end
            end
        end
    end

end

puts maze[dh-1][dw-1] != "." ? route[dh-1][dw-1] : -1
