Multistage Graph Problem Leetcode