BFS with distance was not implemented correctly