Please solve the programming task below using a self-contained code snippet in a markdown code block.

You are given an integer n, which indicates that there are n courses labeled from 1 to n. You are also given an array relations where relations[i] = [prevCoursei, nextCoursei], representing a prerequisite relationship between course prevCoursei and course nextCoursei: course prevCoursei has to be taken before course nextCoursei.
In one semester, you can take any number of courses as long as you have taken all the prerequisites in the previous semester for the courses you are taking.
Return the minimum number of semesters needed to take all courses. If there is no way to take all the courses, return -1.
 
Please complete the following python code precisely:
```python
class Solution:
    def minimumSemesters(self, n: int, relations: List[List[int]]) -> int:
```