Go: package main

import (
	"fmt"
	"sort"
	"strconv"
)

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		str := ([]byte)(strconv.Itoa(v))
		sort.Slice(str, func(i, j int) bool {
			return str[i] < str[j]
		})
		s, _ := strconv.Atoi(string(str))
		sort.Slice(str, func(i, j int) bool {
			return str[i] > str[j]
		})
		l, _ := strconv.Atoi(string(str))
		memo[v] = solve(l-s) + 1
	}
	return memo[v]
}

func main() {
	var v int
	memo = make([]int, 10000)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include<bits/stdc++.h>
using namespace std ;

main(){
	int n ;
	while( cin >> n , n ){
		int p=1000 , cnt=0 ;
		int c[4] = {} ;
		for( int i=0 ; i<4 ; i++ ){
			c[i] = n/p ;
			n -= c[i] * p ;
			p /= 10 ;
		}
		n = 0 ;
		for( int i=0 ; i<4 ; i++ ){
			if( c[0] == c[i] ) cnt++ ;
			n += c[3-i] * pow(10,i) ;
		}
		
		if( cnt == 4 ) cout << "NA" << endl ;
		else {
			cnt = 0 ;
			while(n-6174){
				sort(c,c+4) ;
				int mx=0 , mn=0 ;
				for( int i=0 ; i<4 ; i++ ) mx += c[i] * pow(10,i) ;
				for( int i=0 ; i<4 ; i++ ) mn += c[3-i] * pow(10,i) ;
				n = mx - mn ;
				p = 1000 ;
				for( int i=0 ; i<4 ; i++ ){
					c[i] = n/p ;
					n -= n/p * p ;
					p /= 10 ;
				}
				n = mx - mn ;
				cnt++ ;
			}
			cout << cnt << endl ;
		}
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <numeric>
#include <algorithm>

using namespace std;

int main(int argc, char **argv) {
  while (true) {
    char s[4];
    cin >> s;
    if (strcmp(s, "0000") == 0) {
      break;
    } else {
      int n = atoi(s), count = 0, a, b;

      while (true) {
        if (n == 6174) {
          cout << count << endl;
          break;
        } else if (s[0] == s[1] && s[0] == s[2] &&
                   s[0] == s[3]) {
          cout << "NA" << endl;
          break;
        }
        sort(s, s + 4,  greater<char>());
        //cout << s << endl;                                                                                                                                              
        a = atoi(s);
        //cout << a << endl;                                                                                                                                              
        sort(s, s + 4);
        //cout << s << endl;                                                                                                                                              
        b = atoi(s);
        //cout << b << endl;                                                                                                                                              
        n = a - b;
        count++;
        stringstream ss;
        ss << n;
        strncpy(s, ss.str().c_str(), 4);
      }
    }
  }

  return 0;
}

C++: #include<algorithm>
#include<iostream>
#include<iomanip>
#include<sstream>
#include<string>
#include<cstdlib>

int main()
{
	std::string N;
	while( std::cin >> N, N != "0000" )
	{
		int cnt = 0;

		bool f = true;
		for( int i = 0; i != 3; ++i )
			f &= N[i] == N[i+1];

		if( f )
			std::cout << "NA" << std::endl;
		else
		{
			while( N != "6174" )
			{
				std::string L = N;
				std::sort( L.rbegin(), L.rend() );
				
				std::string S = L;
				std::reverse( S.begin(), S.end() );
				
				std::ostringstream oss;
				oss << std::setw( 4 ) << std::setfill( '0' ) << atoi( L.c_str() ) - atoi( S.c_str() );
				N = oss.str();

				++cnt;
			}

			std::cout << cnt << std::endl;
		}
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
	string s;
	while(cin>>s){
		if(s=="0000")break;
		int ans=0;
		if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){
			ans=-1;
			goto FIN;
		}
		while(s=="6174"){
			string sa,sb;
			char ca[4]={s[0],s[1],s[2],s[3]};
			sort(ca,ca+4);
			for(int i=0;i<4;i++){
			sa.push_back(s[i]);	
			sb.push_back(s[3-i]);
			}
			int a=stoi(sa);
			int b=stoi(sb);
			int c=abs(a-b);
			
			string sc=to_string(c);
			for(int i=0;i<4-sc.size();i++)sc.insert(0,"0");
			s=sc;
			ans++;
			}
			FIN:
			if(ans==-1)cout<<"NA"<<endl;
			else cout<<ans<<endl;
		}
		return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <functional>
using namespace std;

int main()
{
    int num[4];
    int l, s;
    int n;

    scanf("%1d%1d%1d%1d", &num[0], &num[1], &num[2], &num[3]);
    while (num[0] != 0 || num[1] != 0 || num[2] != 0 || num[3] != 0) {
        
        if (num[0] == num[1] && num[1] == num[2] && num[2] == num[3]) {
            printf("NA\n");
        } else {
            int ans = 0;
            n = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];
            
            while (n != 6174) {
                sort(num, num + 4, greater<int>());
                l = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];
                sort(num, num + 4);
                s = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];

                n = l - s;
                num[0] = n / 1000;
                num[1] = (n / 100) % 10;
                num[2] = (n / 10 ) % 10;
                num[3] = n % 10;


                ans++;
            }

            printf("%d\n", ans);
        }

        scanf("%1d%1d%1d%1d", &num[0], &num[1], &num[2], &num[3]);
    }
    
    return 0;
}

C++: #include <stdio.h>
#include <stdlib.h>
const int TARGET = 6174;
const int LEN = 4;

char num[LEN];

void sort(int reverse){
  int i, j, tmp;
  for(i = 0; i < LEN; ++i)
    for(j = i + 1; j < LEN; ++j){
      if((reverse == 0 && num[i] > num[j])
          || (reverse == 1 && num[i] < num[j])){
        tmp = num[i];
        num[i] = num[j];
        num[j] = tmp;
      }
    }
}

void convert(int n){
  int i;
  for(i = LEN - 1; i >= 0; --i){
    num[i] = (n % 10) + '0';
    n /= 10;
  }
}

int isSame(){
  int i, j;
  for(i = 0; i < LEN; ++i)
    for(j = i + 1; j < LEN; ++j)
      if(num[i] != num[j])
        return 0;
  return 1;
}

//int main(void) {
//  int n, i, cnt = 0;
//  scanf("%d", &n);
//  convert(n);
//  if(isSame()){
//    printf("NA\n");
//    return 0;
//  }
//
//  while(atoi(num) != TARGET){
//    sort(0);
//    int s = atoi(num);
//    sort(1);
//    int b = atoi(num);
//    convert(b - s);
//    ++cnt;
//  }
//
//  printf("%d\n", cnt);
//  return 0;
//}
//
int main(void) {
  int n, i, cnt = 0;
  while(1){
    scanf("%d", &n);
    if(!n) break;
    convert(n);
    if(isSame()){
      printf("NA\n");
      return 0;
    }

    while(atoi(num) != TARGET){
      sort(0);
      int s = atoi(num);
      sort(1);
      int b = atoi(num);
      convert(b - s);
      ++cnt;
    }

    printf("%d\n", cnt);
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
  while(1){
  int count=0;
  int N;
  cin>>N;
  if(N==0)
  break;
  if(N%1111==0){
    cout<<"NA"<<endl;
    continue;
  }
   int L,S;
  while(N!=6174){
    int n[4];

   n[0]=N/1000;
   n[1]=N/100%10;
   n[2]=N/10%10;
   n[3]=N%10;

  sort(n,n+4);
  L=1000*n[3]+100*n[2]+10*n[1]+n[0];
  S=1000*n[0]+100*n[1]+10*n[2]+n[3];
  N=L-S;
  count++;
 }
 cout<<count<<endl;
 }
 return 0;
}