  InterviewBit Academy is now Scaler Academy! # Minimum Weight Cycle in an Undirected weighted Graph

Given an integer A, representing number of vertices in a graph.
Given a matrix of integers B of size N x 3 where N represents number of Edges in a Graph and
Triplet (B[i], B[i], B[i]) implies there is an undirected edge between B[i] and B[i]
and weight of that edge is B[i].

Find and return the weight of minimum weighted cycle and if there is no cycle return -1 instead.

Note: Graph may contain multiple edges and self loops.

Input Format

``````The first argument given is the integer A.
The second argument given is the integer matrix B.
``````

Output Format

``````Return the weight of minimum weighted cycle and if there is no cycle return -1 instead.
``````

Constraints

``````1 <= A <= 1000
1 <= B[i], B[i] <= A
1 <= B[i] <= 100000
``````

For Example

``````Input 1:
A = 4
B = [   [1 ,2 ,2]
[2 ,3 ,3]
[3 ,4 ,1]
[4 ,1 ,4]
[1 ,3 ,15]  ]
Output 1:
10

Explanation:
Given graph forms 3 cycles
1. 1 ---> 2 ---> 3 ---> 4 ---> 1 weight = 10
2. 1 ---> 2 ---> 3 ---> 1 weight = 20
3. 1 ---> 3---> 4 ---> 1 weight = 20 