Go: package main

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

func main() {
	var v int
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		count := 0
		for v != 6174 {
			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))
			v = l - s
			count++
		}
		fmt.Println(count)
	}
}

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;
}

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

using namespace std;

int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

int main(void)
{
	int a;
	while (cin >> a, a){
		if (a == 0) break;
		string n = toString(a);
		if (n.length() < 4) {
			for (int i = 0; i <= 4-n.length(); i++) {
				n += '0';
			}
		}
		if (n[0] == n[1] && n[0] == n[2] && n[0] == n[3]){
			cout << "NA" << endl;
			continue;
		}

		int cnt = 0;
		string N = n;
		while (1){
			if (N == "6174"){
				cout << cnt << endl;
				break;
			}
			// cout << N << endl;
			// cin >> tmp;
			string L = N; sort(L.rbegin(), L.rend());
			string S = N; sort(S.begin(), S.end());
			// cout << L << " " << S << endl;
			int d = toInt(L) - toInt(S);
			N = toString(d);
			if (N.length() < 4) {
				for (int i = 0; i <= 4-N.length(); i++) {
					N += '0';
				}
			}
			cnt++;
		}
	}

	return 0;
}