eolymp
bolt
Try our new interface for solving problems
Problems

Jurassic Jigsaw

Jurassic Jigsaw

The famous Jurassic park biologist Dean O'Saur has discovered new samples of what he expects to be the DNA of a dinosaur. With the help of his assistant Petra Dactil, he managed to sequence the samples, and now they are ready for analysis. Dean thinks this dinosaur was affected with a particular disease mutating the DNA of some cells.

To verify his theory, he needs to compute the most likely evolutionary tree from the samples, where the nodes are the samples of DNA. Because there is no temporal data of the DNA samples, he is not concerned where the root of the tree is.

Dean considers the most likely evolutionary tree, the tree with smallest unlikeliness: the unlikeliness of a tree is defined as the sum of the weights of all edges, where the weight of an edge is the number of positions at which the two DNA strings are different.

As a world expert in data trees, he asks you to reconstruct the most likely evolutionary tree.

In the first sample, the optimal tree is AA - AT - TT - TC. The unlikeliness of the edge between AA and AT edge is 1, because the strings AA and AT differ in exactly 1 position. The weights of the other two edges are also 1, so that the unlikeliness of the entire tree is 3. Since there is no tree of unlikeliness less than 3, the minimal unlikeliness of an evolutionary tree for this case is 3.

Input

First line consists of two integers n (1n1000) and k (1k10), the number of samples and the length of each sample respectively. Each of the next n lines contains a string of length k consisting of the characters in ACTG.

Output

On the first line print the minimal unlikeliness of the evolutionary tree. Then print n1 lines, each consisting of two integers u, v (0u, v < n), indicating that in the most likely evolutionary tree, there is an edge between DNA string u and v. If there are multiple answers possible, any of them will be accepted.

Time limit 1 second
Memory limit 128 MiB
Input example #1
4 2
AA
AT
TT
TC
Output example #1
3
0 1
1 2
2 3
Input example #2
4 1
A
A
G
T
Output example #2
2
0 1
0 2
0 3
Input example #3
5 6
GAACAG
AAAAAA
AACATA
GAAAAG
ATAAAT
Output example #3
7
0 3
1 2
1 3
1 4
Source 2018 Benelux Algorithm Programming Contest (BAPC), Preliminaries, Problem J