k-Gorets
Shaman Slyzhky Maktsakhmetl is performing a training ritual known as "k-Highlander". Each of the n tribe members has received k clay tablets, each indicating which fellow tribe member they should follow. Every tablet originally had a feather of one of the k colors attached. Tablets with the same feather color formed cycles, ensuring that each of the n participants appeared in exactly one cycle per color.
Unfortunately, a strong mountain wind blew all the feathers away, scattering them into the desert. Your task is to help Maktsakhmetl reattach the feathers to the tablets so that, for each color, cycles are reformed with each participant included exactly once.
Input
The first line of the input contains two integers, n and k, representing the number of tribe members and the number of feather colors, respectively (1 ≤ n ≤ 500, 1 ≤ k ≤ 20).
Each of the following n lines contains k integers ranging from 1 to n. These numbers indicate the tribe members that each participant was supposed to follow. It is guaranteed that no tribe member was assigned to follow themselves. It is possible for a tribe member to have multiple tablets pointing to the same target.
It is assured that there exists a way to reassign the feathers to the tablets as required.
Output
Output the table in the same format, rearranging the numbers in each row so that the i-th column corresponds to tablets with feathers of color i. These should form one or more cycles, with each participant included exactly once. If multiple solutions exist, you may output any one of them.