INF = 1000000000
H, W = gets.chomp.split.map(&:to_i)
sy, sx = gets.chomp.split.map(&:to_i)
sy -= 1
sx -= 1
gy, gx = gets.chomp.split.map(&:to_i)
gy -= 1
gx -= 1
maze = []
H.times do
    maze << gets.chomp.split("")
end
reach = Array.new(H).map{Array.new(W, INF)}
vis = Array.new(H).map{Array.new(W, false)}
queue = [[sx, sy]]
reach[sy][sx] = 0
until queue.empty?
    x, y = queue.shift
    next if vis[y][x]
    vis[y][x] = true
    break if x == gx && y == gy
    [
        [x-1, y],
        [x+1, y],
        [x, y-1],
        [x, y+1]
    ].each do |cx, cy|
        next if cx < 0 || cx >= W || cy < 0 || cy >= H || maze[y][x] == "#"
        if reach[y][x] < reach[cy][cx] then
            reach[cy][cx] = reach[y][x]
            queue.unshift([cx, cy])
        end
    end
    (-2..2).each do |h|
        (-2..2).each do |w|
            cx = x + w
            cy = y + h
            next if cx < 0 || cx >= W || cy < 0 || cy >= H || maze[y][x] == "#"
            if reach[y][x] + 1 < reach[cy][cx] then
                reach[cy][cx] = reach[y][x] + 1
                queue << [cx, cy]
            end
        end
    end
end

puts reach[gy][gx] != INF ? reach[gy][gx] : -1