Adopting the code to arbitrary number of vertices