Bear and Two Paths

CodeForces - 673D