For each digit i from 1 to 9, let Di be the number of times i occurs in the decimal representation of N. You write out, in ascending order, every positive integer whose decimal representation contains the digit i exactly Di times for every i from 1 to 9. The number of times the digit 0 occurs is not restricted. No number has a leading zero.
For example, if you are writing every number with two 1s and one 5, your list starts 115, 151, 511, 1015, 1051.
You are given N, the last number you wrote. Compute the next number in the list.