INF = 1 << 40
H, W = gets.chomp.split.map(&:to_i)
sy, sx = gets.chomp.split.map(&:to_i)
gy, gx = gets.chomp.split.map(&:to_i)
maze = []
H.times do
    maze << gets.chomp.split("")
end
queue = [[sx-1, sy-1]]
reach = Array.new(H).map{Array.new(W, INF)}
reach[sy-1][sx-1] = 0
vis = Array.new(H).map{Array.new(W, false)}
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 |nx, ny|
        next if nx < 0 || W <= nx || ny < 0 || H <= ny
        next if maze[ny][nx] == "#"
        if reach[y][x] < reach[ny][nx] then
            queue.unshift([x, y])
            reach[ny][nx] = reach[y][x]
        end
    end
    (-2..2).each do |i|
        (-2..2).each do |j|
            nx, ny = x+j, y+i
            next if nx < 0 || W <= nx || ny < 0 || H <= ny
            next if maze[ny][nx] == "#"
            if reach[y][x] + 1 < reach[ny][nx] then
                queue << [x, y]
                reach[ny][nx] = reach[y][x] + 1
            end
        end
    end
end

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