Time limit
5s
Memory limit
512 MB
A monkey that has left the zoo wants to share its thoughts with people, but people cannot understand the monkey's language.
The monkey therefore creates a new way to send messages using a drum. The monkey understands the digits from 0 to K-1. The drum has K^M cells, and each cell contains one number between 0 and K-1. By rotating the drum one cell at a time, the monkey reads the M consecutive cells visible at once and sends those M numbers as one message.
When K=3 and M=2, there are 3^2 messages of length 2, from 00 through 22. With a suitable arrangement on the drum, the M consecutive numbers read after each one-cell rotation can represent every possible message exactly once.
Given K and M, determine whether such a drum can be made for every length-M message over the digits 0 through K-1. If it can, output one valid sequence to write on the drum.
The first line contains two natural numbers K and M. K is at least 2, and M is at least 1. It is guaranteed that K^M <= 10,000,000.
Print the K^M numbers written on the drum in order, separated by spaces. You may use either clockwise or counterclockwise order, and you may start from any cell as long as you print exactly one full rotation. If there are multiple valid answers, print any one of them. If no such drum can be made, print -1.