Problem Statement : The NISER Campus Network

NISER is upgrading its campus network to provide seamless WiFi connectivity across all buildings. The goal is to connect N buildings in the campus with optical fiber cables. However, due to budget constraints, the total cost of the cables must be minimized.

Each pair of buildings can be connected by a direct cable of a certain length, which corresponds to the cost of laying that cable. The campus planning team has surveyed the buildings and provided a list of potential cable connections along with their costs. However, not all pairs of buildings need to be directly connected; the network only needs to be fully connected, meaning there should be a way to reach any building from any other building.

Your task is to help the planning team determine the minimum cost required to lay the cables such that all buildings are connected.

Input Format

Output Format

Output a single integer: the minimum cost to connect all the buildings. If it’s not possible to connect all buildings, output -1.

Constraints

Example

Input

4 5
1 2 4
1 3 3
2 3 2
2 4 5
3 4 1

Output

6

Explanation

The optimal network connects the buildings as follows:

The total cost is 1 + 2 + 3 = 6.

Good Luck and Happy Coding

Solution

The solution to the problem can be found at Solution 3.