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.Print("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 <algorithm>
#include <functional>
#include <sstream>
#include <numeric>
#include <cstdio>
#include <cassert>
using namespace std;

string to_str(int n)
{
	string res;
	res.push_back('0' + n % 10000 /1000);
	res.push_back('0' + n % 1000 / 100);
	res.push_back('0' + n % 100 / 10);
	res.push_back('0' + n % 10 / 1);
	return res;
}

bool same_all(string s) { return count(s.begin(), s.end(), s[0]) == s.size(); }
int to_int(string s) { return atoi(s.data()); }

int main()
{
	for(string line; cin >> line && (line != "0000"); ) {
	  int times = 0;

		if(same_all(line)) {
			cout << "NA" << endl;
			continue;
		}
		
		for(; line != "6174"; times++) {
			sort(line.begin(), line.end());
			int small = to_int(line);
		
			reverse(line.begin(), line.end());
			int big = to_int(line);

			int next = big - small;

			line = to_str(next);
		}

		cout << times << endl;
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
	
int main(){
	while(1){
		int N,cnt;
		cin >>N;

		if(N==0000)
			break;

		if(N%1111==0){
			cout << "NA" <<endl;
			continue;
		}

		while(N!=6174){
			int n[4]={N/1000,N%1000/100,N%100/10,N%10};
			sort(n,n+4);
			int L=n[3]*1000+n[2]*100+n[1]*10+n[0];
			int S=n[0]*1000+n[1]*100+n[2]*10+n[3];
	
			N=L-S;
			cnt++;
		}
	
		cout <<cnt<<endl;

	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;int main(){int n;while(true){cin>>n;if(n==0)break;if(n%1111==0)cout<<"NA"<<endl;else{int ans=0,count,s[4];while(true){if(n==6174)break;int a=10;for(int i=0;i<4;i++){s[i]=n%a/(a/10);a*=10;}sort(s,s+4);count=0;a=1;for(int i=0;i<4;i++){count+=a*s[i]-a*s[3-i];a*=10;}n=count;ans++;}cout<<ans<<endl;}}return 0;}

C++: #include<iostream>
using namespace std;
int main()
{
    int input;
    int digit[4];
    int max, min;
    int base[4]={1000,100,10,1};
    int c=0,i,j,tmp;

    cin >> input;
    while(input != 0)
    {
        if(input % 1111 == 0){
	    cout << "NA" << endl;
	    cin >> input;
	    continue;
        }
	while(input != 6174){
            for(i=0; i<4; i++) // 各桁を取り出す
            {
                digit[i]= input / base[i];
                input = input % base[i]; // Highestを除く
            }
            for(i=0;i<3;i++) // sort
            {
                for(j=i+1;j<4;j++)
                {
                     if(digit[i]<digit[j])
                    {
                        tmp=digit[i];
                        digit[i]=digit[j];
                        digit[j]=tmp;
                    }
                }
            }
	    max = min = 0;
            for(i=0;i<4;i++)
            {
                max += (digit[i]*base[i]);
                min += (digit[i]*base[3-i]);
            }
            input = max-min;
            c++;
	}
	//
        cout << c << endl;
	c = 0;
        cin >> input;
    }
    return 0;
}

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

const string ANS = "6174";
const int N = 4;

int main(int argc, char const* argv[])
{
	string buf;
	int c;
	int l[N], s[N], res[N];
	bool na;
	while(cin >> buf){
		c = 0;
		na = true;
		if(buf == "0000") break;
		for(int i(0);i < N - 1;++i){
			na = na && (buf[i] == buf[i + 1]);
		}
		if(na) {
			cout << "NA" << endl;
			continue;
		}
		while(buf != ANS){
			for(int i = 0;i < N;++i){
				l[i] = s[i] = (int)(buf[i] - '0');
			}
			sort(l, l + N, greater<int>());
			sort(s, s + N);
			for(int i = N - 1;0 <= i;--i){
				res[i] = l[i] - s[i];
				if(res[i] < 0){
					--l[i - 1];
					res[i] += 10;
				}
			}
			for(int i = 0;i < N;++i){
				buf[i] = (char)(res[i] + '0');
			}
			++c;
		}
		cout << c << endl;
		
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <functional>
#include <cmath>
using namespace std;

int main(void) {
	int S, L;
	string N;
	int ans;
	bool flag = false;

	while(1) {
flag=false;
		cin >> N;
		if(N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
			flag = true;
			if(N[0] == '0')
				break;
		}

		if(!flag) {
			for(ans = 0; atoi(N.c_str()) != 6174; ans++) {
				sort(N.begin(), N.end());
				S = atoi(N.c_str());
				sort(N.begin(), N.end(), greater<int>());
				L = atoi(N.c_str());
				int tmp = L - S;

				for(int i = 1000, j = 0; i; i /= 10, j++) {
					N[j] = tmp / i + '0';
					tmp %= i;
				}
			}
		}

		if(flag)
			cout << "NA" << endl;
		else
			cout << ans << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string Int2Str(int);
int Str2Int(string);
int main(){
  int l,s,cal;
  string str,L,S;
  while(1){
    cin >> str;
    if(str=="0000") break;
    int cnt=0,flag=0;
    if(str=="6174") cnt--;
    while(1){
      cnt++;
      sort(str.begin(),str.end());
      S=str;
      reverse(str.begin(),str.end());
      L=str;
      l=Str2Int(L);
      s=Str2Int(S);
      cal=l-s;
      if(cal==6174){
	flag=1;
	break;
      }
      if(cal==0||cal==1111||cal==2222||cal==3333||cal==4444||cal==5555||cal==6666||cal==7777||cal==8888||cal==9999){
	break;
      }
      str=Int2Str(cal);
    }
    if(flag==1) cout << cnt << endl;
    else cout << "NA" << endl;
  }
  return 0;
}
string Int2Str(int n){
  int p;
  string ret;
  while(n){
    p=n%10;
    ret=(char)(p+'0')+ret;
    n/=10;
  }
  if(ret.size()!=4){
    while(ret.size()!=4){
      ret+='0';
    }
  }
  return ret;
}
int Str2Int(string str){
  int ret=0,i=0,x=1,len=4;
  while(len){
    ret=ret*x+(str[i]-'0');
    i++;
    x=10;
    len--;
  }
  return ret;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define reps(i, m, n) for (int i = m; i <= n; ++i)
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;

signed main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  while (cin >> N, N) {
    int cnt = 0;
    bool isNA = false;
    while (N != 6174) {
      int tmp[20];
      rep(i, 4) {
        tmp[i] = N % 10;
        N /= 10;
      }
      if( tmp[0] == tmp[1] && tmp[1] == tmp[2] && tmp[2] == tmp[3] ) {
        isNA = true;
        break;
      }
      else {
        sort(tmp, tmp + 4, greater<int>());
        int L = tmp[0] * 1000 + tmp[1] * 100 + tmp[2] * 10 + tmp[3];
        int S = tmp[3] * 1000 + tmp[2] * 100 + tmp[1] * 10 + tmp[0];
        N = L - S;
        ++cnt;
        if( N == 6174 ) break;
      }
    }
    if( isNA ) cout << "NA" << endl;
    else cout << cnt << endl;
  }
  return 0;
}