human
Give me a the least number of buses we must take to reach our destination given the following:
Solve in Python:
We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... forever.
We start at bus stop S (initially not on a bus), and we want to go to bus stop T. Travelling by buses only, what is the least number of buses we must take to reach our destination? Return -1 if it is not possible.
Example:
Input: 
routes = [[1, 2, 7], [3, 6, 7]]
S = 1
T = 6
Output: 2
Explanation: 
The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6.

 
Constraints:

1 <= routes.length <= 500.
1 <= routes[i].length <= 10^5.
0 <= routes[i][j] < 10 ^ 6.
assistant
#Here is the least number of buses we must take to reach our destination.

class Solution:
    def numBusesToDestination(self, routes: List[List[int]], S: int, T: int) -> int:
        graph = collections.defaultdict(list)
        if S == T: return 0
        nodeS, nodeT = set(), set()
        for i in range(len(routes)):
            if S in routes[i]: nodeS.add(i)
            if T in routes[i]: nodeT.add(i)
            if i < len(routes) - 1:
                for j in range(1, len(routes)):
                    if set(routes[i]) & set(routes[j]): 
                        graph[i].append(j)
                        graph[j].append(i)
        
        # print(graph)
        if nodeS & nodeT: return 1
        if not nodeS or not nodeT: return -1
                
        queue, visited = collections.deque(), set()
        for s in nodeS:
            queue.append((s, 1))
        
        
        
        while queue:
            # print(queue)
            node, step = queue.popleft()
            if node in nodeT: return step
            for nbr in graph[node]:
                if nbr not in visited:
                    queue.append((nbr, step + 1))
                    visited.add(nbr)
        
        return -1