Count reachable nodes from every node .
let visited = new Array ( n + 1 ) ; let ans = 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ! visited [ i ] ) { ans = lcm ( ans , dfs ( i , adj , visited ) ) ; } } return ans ; }