Given
n courses (1..n) and dependencies relations of the form [prev, next]. Return the minimum number of semesters to complete all courses, or -1 if impossible.Example 1
Input: n = 3, relations = [[1,3],[2,3]]
Output: 2
Explanation: Two semesters: courses 1 and 2 in parallel, then 3.
Output: 2
Explanation: Two semesters: courses 1 and 2 in parallel, then 3.
Example 2
Input: n = 3, relations = [[1,2],[2,3],[3,1]]
Output: -1
Explanation: Cycle — impossible to complete.
Output: -1
Explanation: Cycle — impossible to complete.