working solution with BFS + map/reduce