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 <algorithm>
using namespace std;

int main(){
	int num[4],l,s,aa=0,n;
	while(cin>>n,n){
		if(n>999){
			num[0]=n/1000;
			num[1]=n/100%10;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n>99&&n<1000){
			num[0]=0;
			num[1]=n/100%10;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n>9&&n<100){
			num[0]=0;
			num[1]=0;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n<10){
			num[0]=0;
			num[1]=0;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		aa=0;
		if(num[0]==num[1]&&num[1]==num[2]&&num[2]==num[3]){
			cout<<"NA"<<endl;
			break;
		}
		while(num[0]!=6&&num[1]!=1&&num[2]!=7&&num[3]!=4){
			l=0,s=0;
			sort(num,num+4);
			for(int i=0;i<4;i++)cout<<num[i];
			cout<<endl;
			s=num[0]*1000+num[1]*100+num[2]*10+num[3];
			l=num[3]*1000+num[2]*100+num[1]*10+num[0];
			num[0]=(l-s)/1000;
			num[1]=(l-s)/100%10;
			num[2]=(l-s)/10%10;
			num[3]=(l-s)%10;
			aa++;
		}
		cout<<aa<<endl;
	}
}

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

int main() {
	string N;
	
	while (cin >> N) {
		if (N == "0000") break;
		int ok = 1, ans = 0;
		set< string > strs;
		
		while (ok) {
			if (N == "6174") break;
			strs.insert(N);
			
			string L = N, S = N;
			sort(L.begin(), L.end(), greater< char >());
			sort(S.begin(), S.end());
			
			stringstream ss;
			ss << L << " " << S;
			int l, s;
			ss >> l >> s;
			
			int n = l - s;
			stringstream sss;
			sss << n;
			N = sss.str();
			
			if (strs.count(N)) ok = 0;
			
			++ans;
		}
		
		if (ok) cout << ans << endl;
		else cout << "NA" << endl;
	}
}

C++: #include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;



signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N = 1;
	while (N) {
		string S; cin >> S;
		N = (S[0] - '0') * 1000 + (S[1] - '0') * 100 + (S[2] - '0') * 10 + (S[3] - '0');
		if (!N)break;
		VI A(4), B(4);
		A[0] = B[0] = N % 10;
		A[1] = B[1] = (N / 10) % 10;
		A[2] = B[2] = (N / 100) % 10;
		A[3] = B[3] = N / 1000;
		SORT(A);
		rSORT(B);
		if (A == B) {
			cout << "NA" << '\n';
			continue;
		}
		int ans = 0;
		while (N != 6174) {
			ans++;
			int b = B[0] * 1000 + B[1] * 100 + B[2] * 10 + B[3];
			int a = A[0] * 1000 + A[1] * 100 + A[2] * 10 + A[3];
			N = b - a;
			A[0] = B[0] = N % 10;
			A[1] = B[1] = (N / 10) % 10;
			A[2] = B[2] = (N / 100) % 10;
			A[3] = B[3] = N / 1000;
			SORT(A);
			rSORT(B);
		}
		cout << ans << endl;
	}

	return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int main(){
	int n;
	int cnt;
	int num[4];
	while(scanf("%d",&n)&&n){
		cnt = 0;
		if(n%1111==0){
			cout <<"NA"<<endl;
			continue;
		}
		while(n!=6174){
			cnt++;
			int tmp =n;
			for(int i =0;i <4;i++){
				num[i]=tmp%10;
				tmp /=10;
			}
			int x = 0;
			sort(num,num+4,greater<int>());
			for(int i =0;i <4;i++){
				x =x*10+num[i];
			}
			int L,S;
			L =x;
			sort(num,num+4);
			x = 0;
			for(int i =0;i <4;i++){
				x =x*10+num[i];
			}
			S =x;
			n =L-S;
		}
		cout <<cnt<<endl;
	}
	return 0;
}

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

int main()
{
  int n;
  int l, s;
  vector<int> vi1;
  vector<int> vi2;
  int cnt;

  while(1){
    cin >> n;
    if(n == 0) break;

    cnt = 0;

    while(1){
      l = s = 0;
      vi1.clear();
      vi2.clear();
      if(n == 6174){
	cout << cnt << endl;
	break;
      }else if(n == 0){
	cout << "NA" << endl;
	break;
      }

      for(int i = 0;i < 4;++i){
	vi1.push_back(n % 10);
	vi2.push_back(n % 10);
	n /= 10;
      }
      
      //l,sを求める
      sort(vi1.begin(), vi1.end());
      sort(vi2.begin(), vi2.end(), greater<int>());

      /*
      cout << "vi1:";
      for(int i = 0;i < 4;++i){
	cout << vi1[i] << ' ';
      }
      cout << endl;
      cout << "vi2:";
      for(int i = 0;i < 4;++i){
	cout << vi2[i] << ' ';
      }
      cout << endl;
      */
      
      for(int i = 0;i < 4;++i){
	l *= 10;
	l += vi1[i];
	
	s *= 10;
	s += vi2[i];
      }

      /*
      cout << s << "-" << l << "=" << s - l << endl;
      */
      
      //新しいNを求める
      n = s - l;
      ++cnt;
    }

  }

  return 0;
}

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

#define DEBUG(x) cerr << #x << " = " << x << endl

template<class T, class U> T lexical_cast(const U &from) {
    stringstream ss;
    ss << from;
    T to;
    ss >> to;
    return to;
}

bool same(const string &s) {
    return s[0] == s[1] && s[0] == s[2] && s[0] == s[3];
}

int main() {
    for(string S; cin >> S, S != "0000"; ) {
        if(same(S)) {
            cout << "NA" << endl;
            continue;
        }
        int ans = 0;
        while(S != "6174") {
            sort(begin(S), end(S));
            string L = "    ";
            for(int i = 0; i < 4; i++) L[i] = S[3 - i];
            S = lexical_cast<string>(lexical_cast<int>(L) - lexical_cast<int>(S));
            ans++;
        }
        cout << ans << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int a;
  int b=0;
  int c[8],d[8];
  int h,l;
  while(true){
    b=0;
    cin>>a;
    if(a==0000){
      break;
    }
    while(true){
      if(a==6174){
	cout<<b<<"\n";
	break;
      }
      if(a==0){
	cout<<"NA\n";
	break;
      }
      for(int i=0;i<4;i++){
	c[i]=a%10;
	a=a/10;
	d[i]=c[i];
      }
      sort(c,c+4);
      sort(d,d+4,greater<int>());
      for(int i=0;i<4;i++){
	if(i==0){
	h=c[i]*1000;
	l=d[i]*1000;
	}
	if(i==1){
	   h=c[i]*100+h;
	   l=d[i]*100+l;
	}
	if(i==2){
	  h=c[i]*10+h;
	  l=d[i]*10+l;
	}
	if(i==3){
	  h=c[i]+h;
	  l=d[i]+l;
	}
      }
      a=l-h;
      b++;
    }
  }
  return(0);
}