The Ouroboros problem

This symbol appears principally among the Gnostics and is depicted as a dragon, snake or serpent biting its own tail. In the broadest sense, it is symbolic of time and the continuity of life. Similarly, we can make a “digital ouroboros” in the shape of a ring with a property: if you take M adjacent digits, they form a different permutation of M digits, without an established order, but including every legal permutation. The number is represented in a given base N. The minimum value for N and M is 1, and the maximum value for both of them is 10. NM should be less than 65536. For example: With M = 2 and N = 3, a possible solution is: 001122102 from which you can obtain (00, 01, 11, 12, 22, 21, 10, 02, 20) by taking the first two digits, the second and the third, and so on. The last number is built by linking the last and first digits of the string. Input The input consists on a list of pairs of numbers (M,N), where M is the amount of digits we are going to deal with, and N the base of the numbers. Output The output must be a string with one of the possible ouroboros. Sample Input 33 42 Sample Output 000111222121102202101201002 1111000010100110