/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

import java.util.*;

public class Main
{
    static String subtractionResult;
    static int executions;
    static int temp;
    static int[] ascending = new int[4];
    static int[] descending = new int[4];
    static int count=1;
    static int subtraction
    static StringJoiner sj = new StringJoiner("");
    static StringJoiner sj1 = new StringJoiner("");
    static int lastDigit=0;
    static int N=1010;
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        System.out.println("Number executions:" + KaprekarConstant(N, ascending));
    }
    
    public static int KaprekarConstant(int number, int[] ascending)
    {
        if ((number/10)==0) 
        {
            executions++;
            ascending[0]=number;
            
            for (int k=0; k<(ascending.length-1); k++)
            {
                if (ascending[k]>ascending[k+1])
                {
                    temp=ascending[k+1];
                    ascending[k+1]=ascending[k];
                    ascending[k]=temp;
                }
            }
            
            for (int m=0;m<ascending.length;m++)
            {        
                descending[(descending.length-1)-m]=ascending[m];
            }
            
            for (int p: ascending)
            {
                sj.add(String.valueOf(p));
            }
            
            for (int p: descending)
            {
                sj1.add(String.valueOf(p));
            }
            
            subtraction = Math.subtractExact(Integer.valueOf(sj1.toString()), Integer.valueOf(sj.toString()));
            subtractionResult=Integer.toString(subtraction);
            
            if (subtractionResult.length()<4)
            {
                System.out.println((sj1.toString() + "-" + (sj.toString() + "=" + "0"+subtractionResult)));
            }
            else
            {
                System.out.println((sj1.toString() + "-" + (sj.toString() + "=" + subtraction)));
            }
            
            if (subtraction==6174)
            {
                return executions;
            }
            else
            {
                count=1;
                sj=new StringJoiner("");
                sj1=new StringJoiner("");
                ascending = new int[4];
                descending = new int[4];
                return KaprekarConstant(subtraction, ascending);
            }
        }
        lastDigit=number%10;
        ascending[(ascending.length-count)]=lastDigit;
        
        for (int k=(ascending.length-1); k>0; k--)
        {
            if (ascending[k]<ascending[k-1])
            {
                temp=ascending[k-1];
                ascending[k-1]=ascending[k];
                ascending[k]=temp;
            }
        }
        count++;
        
        return KaprekarConstant(number/10, ascending);
    }
}