After performing so badly in the World Cup, Mushfiq left cricket and started problem solving. You told him to solve the following problem.
You are given a rooted tree with vertices (each edge of the tree has a cost) and a magic integer . The vertices are numbered from to , the root is the vertex number . You are also given an operation —
You have to find two integers and such that length of the path from root to is maximum, GCD of the costs of the path is a multiple of by applying the operation at most once and is minimum. If there are multiple nodes with maximum path length and minimum print the node with minimum label.
The first line contains two space separated integers and — the number of vertices and the magic integer.
Each of the next lines describes an edge of the tree. Edge is denoted by three integers , and — the labels of vertices it connects and cost of the edge.
Print two integers and — if length of the path from root to is maximum, GCD of the path is a multiple of and is minimum.
Input | Output |
---|---|
9 6 1 2 6 1 3 6 2 4 6 2 5 6 3 6 12 3 7 18 5 8 8 5 9 7 | 8 3 |
Test Case 1 Tree |