/*

 * Andrew Imm

 * TJHSST

 * Junior Division - ACSL Contest #4

 * April 2007

 * "FLOCK"

 */

 

 

import javax.swing.JOptionPane;

import java.lang.*;

 

public class ACSL4

{

            public static void main(String[] args)

            {

                        while(true)

                        {

                                    String input = JOptionPane.showInputDialog("Please enter the input. Separate each number with only a comma. Do not use spaces. (eg. '3,1,3,5,3')");

                                    boolean[] stage = new boolean[10];

                                    boolean[] stageCopy = new boolean[10];

                                    int total = 0;

                                    int z = 0;

                                    for(int y = 0; y < input.length(); y++)

                                    {

                                                if(input.charAt(y)==',')

                                                {

                                                            z = y+1;

                                                            total = Integer.parseInt(input.substring(0,y));

                                                            y = input.length();

                                                }

                                    }

                                    for(int n = 0; n < input.length(); n++)

                                    {

                                                for(int y = z; y < input.length(); y++)

                                                {

                                                            if(input.charAt(y)==',')

                                                            {

                                                                        int tmp = Integer.parseInt(input.substring(z,y))-1;

                                                                        stage[tmp] = true;

                                                                        z = y+1;

                                                            }

                                                }

                                    }

                                    int steps = Integer.parseInt(input.substring(z,input.length()));

                                    for(int n = 0; n < steps-1; n++)

                                    {

                                                for(int m = 0; m < 10; m++)

                                                            stageCopy[m] = stage[m];

                                                int s = 0;

                                                if((stage[9] == true)&&(stage[0] == false))

                                                {

                                                            stageCopy[0] = true;

                                                            stageCopy[9] = false;

                                                            stage[9] = false;

                                                            s = 1;

                                                }

                                                for(int p = s; p < 9; p++)

                                                {

                                                            if(stage[p] == true)

                                                            {

                                                                        if(stage[p+1] == false)

                                                                        {

                                                                                    stageCopy[p] = false;

                                                                                    stageCopy[p+1] = true;

                                                                        }

                                                                        else

                                                                        {

                                                                                    stageCopy[p] = true;

                                                                        }

                                                            }

                                                }

                                                for(int m = 0; m < 10; m++)

                                                            stage[m] = stageCopy[m];

                                    }

                                    int c = 0;

                                    for(int n = 0; n < 10; n++)

                                    {

                                                if(stageCopy[n])

                                                {

                                                            c++;

                                                            System.out.print(n+1);

                                                            if(c < total)

                                                                        System.out.print(",");

                                                }

                                    }

                                    System.out.println();

                        }

            }

}