Go: package main

import (
	"fmt"
	"sort"
)

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	//if memo[v] == 0 {
	a := make([]int, 4)
	for i := 0; i < 4; i++ {
		a[i] = v % 10
		v /= 10
	}
	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})
	l := int(0)
	for i := 0; i < 4; i++ {
		l *= 10
		l += a[i]
	}
	sort.Slice(a, func(i, j int) bool {
		return a[i] < a[j]
	})
	s := int(0)
	for i := 0; i < 4; i++ {
		s *= 10
		s += a[i]
	}
	memo[v] = solve(l-s) + 1
	//}
	//return memo[v]
	return solve(l-s) + 1
}

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;

#define ALL(v) (v).begin(), (v).end()

int rec(string N, int cnt) {
  vector<char> LN(4), SN(4);
  int L = 0, S = 0;
  for (int i = 0; i < 4; i++) {
    LN[i] = SN[i] = N[i] - '0';
  }
  sort(ALL(LN));
  sort(ALL(SN));
  for (int i = 0; i < 4; i++) {
    L *= 10;
    S *= 10;
    L += LN[3 - i];
    S += SN[i];
  }
  return (L - S == 6174) ? cnt + 1 : rec(to_string(L - S), cnt + 1);
}

signed main()
{
  string N;
  while (cin >> N, N != "0000") {
    if (N[0] == N[1] and N[2] == N[3] and N[0] == N[2]) cout << ("NA");
    else if (N == "6174") cout << ("0");
    else cout << rec(N, 0);
    cout << "\n";
  }

  return (0);
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str(); }
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 2;
const int MOD = 1000000007;

using namespace std;
typedef vector<ll> vl;
typedef vector<vl> vvl;

int main() {
	string s;
	while (cin >> s) {
		if (s == "0000") {
			break;
		}
		int ans = 0;
		if (toInt(s) % 1111 == 0) {
			cout << "NA" << endl;
		}
		else {
			while (s != "6174") {
				string a = s, b = s;
				stable_sort(ALL(a));
				stable_sort(RALL(b));
				s = toStr(toInt(b) - toInt(a));
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    int N,S = 0;
   while(1)
   {
       cin >> N;
       int big[4] = {0},small[4],ans = 0;
       if(N == 0000)break;
       if(N % 1111 == 0)
       {
           puts("NA");
       }
       else
       {
           while(N != 6174)
           {


               big[0] = small[0] = N / 1000;
               big[1] = small[1] = (N - (big[0]*1000)) / 100;
               big[2] = small[2] = (N - (big[0]*1000 + big[1]*100)) / 10;
               big[3] = small[3] = N % 10;
               /*for(int i = 0; i < 4; i++)
               {
                   cout << big[i] << "??¨" << small[i] << endl;
               }*/
               for(int i = 0; i < 4; i++)
               {
                   for(int j = i+1; j < 4; j++)
                   {
                       if(big[i] > big[j])swap(big[i],big[j]);
                       if(small[i] < small[j])swap(small[i],small[j]);
                   }
               }
               N = 0; S = 0;
               int aaa = 1;
               for(int i = 0; i < 4; i++)
               {

                   S += small[i]*aaa;
                   N += big[i]*aaa;
                   aaa = aaa*10;
               }
               N -= S;
               ans++;
           }
           cout << ans << endl;
       }

   }
}

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

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int main()
{
	string s;
	while(cin >> s)
	{
		if(s == "0000") break;
		
		bool flag = true;
		rep(i,s.size()) if(s[0] != s[i]) flag = false;

		if(flag)
		{
			cout << "NA" << endl;
		}
		else
		{
			int cnt = 0;
			while(true)
			{
				if(s == "6174")
				{
					cout << cnt << endl;
					break;
				}
			
				string temp = s,temp2 = s;
				sort(temp.begin(),temp.end());
				sort(temp2.begin(),temp2.end(),greater<char>());

				int t = atoi(temp.c_str());
				int t2 = atoi(temp2.c_str());

				stringstream ss;
				ss << t2 - t;

				s = "";
				REP(i,ss.str().size(),4) s += "0";

				s += ss.str();
				cnt++;
			}
		}

	}
}

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

int main() {
	int N, x = 0;
	vector<int> n(4);
	vector<int> m(4);

	cin >> N;
	n.at(0) = N / 1000;
	n.at(1) = (N - n.at(0) * 1000) / 100;
	n.at(2) = (N - n.at(0) * 1000 - n.at(1) * 100) / 10;
	n.at(3) = N - n.at(0) * 1000 - n.at(1) * 100 - n.at(2) * 10;

	for (;;){
		sort(n.begin(), n.end());

		for (int i = 0; i < 4; i++)
			m.at(i) = n.at(i);

		reverse(m.begin(), m.end());

		for (int i = 0; i < 4; i++)
			n.at(i) = m.at(i) - n.at(i);

		x++;

		if (n.at(0) == 6 && n.at(1) == 1 && n.at(2) == 7 && n.at(3) == 4){
			cout << x;
			break;
		}
	}
}

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

template <class F, class T>
void convert(const F &f, T &t){
	stringstream ss;
	ss << f;
	ss >> t;
}

int main(){
	int x, a, b;
	string s;
	while(scanf("%d", &x), x){
		if(x % 1111 == 0){
			puts("NA");
		}
		else{
			int ans;
			for(ans = 0; x != 6174; ++ans){
				convert(x, s);
				sort(s.begin(), s.end());
				convert(s, a);
				reverse(s.begin(), s.end());
				convert(s, b);
				x = b - a;
			}
			printf("%d\n", ans);
		}
	}
}

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