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>
#include <string>
#include <vector>
#include <functional>
#include <queue>
#include <set>
#include <map>
#include <numeric>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <sstream>

#define rep(i, a) REP(i, 0, a)
#define REP(i, a, b) for(int i = a; i < b; ++i)
#define repll(i, a) REPLL(i, 0, a)
#define REPLL(i, a, b) for(ll i = a; i < b; ++i)

typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int, int> P;
typedef std::pair<P, int> PP;
const double PI = 3.14159265358979323846;
const double esp = 1e-9;
const int infi = (int)1e+9 + 10;
const ll infll = (ll)1e+17 + 10;

std::string n, l, s;
int cnt = 0;
bool used[10000];
bool flg = true;

int main(){
	while (std::cin >> n, n != "0000"){
		rep(i, 10000)used[i] = false;
		cnt = 0;
		flg = true;
		while (n != "6174"){
			l = n;
			s = n;
			std::sort(l.begin(), l.end(), std::greater<char>());
			std::sort(s.begin(), s.end());
			n = std::to_string(std::stoi(l) - std::stoi(s));
			if (used[std::stoi(n)]){
				flg = false;
				break;
			}
			used[std::stoi(n)] = true;
			++cnt;
		}
		if (flg)std::cout << cnt << std::endl;
		else std::cout << "NA" << std::endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<sstream>
#include<string>
#include<cstdlib>
#define rep(i,a) for( int i = 0; i != (a); ++i )

std::string intToStr( int n )
{
	std::ostringstream oss;
	oss << n;

	return oss.str();
}

int main()
{
	std::string L, S;
	while( std::cin >> L, L != "0000" )
	{
		bool fl = true;
		rep( i, 3 )
			fl &= L[i+1] == L[i];
		if( fl )
		{
			std::cout << "NA" << std::endl;
			continue;
		}

		int ans = 0;
		while( L != "6174" )
		{
			S = L;
			std::sort( L.rbegin(), L.rend() );
			std::sort( S.begin(), S.end() );

			++ans;

			L = intToStr( atoi( L.c_str() ) - atoi( S.c_str() ) );
		}

		std::cout << ans << std::endl;
	}

	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int N;
int ans = 0;
int main()
{
	while (true){
		cin >> N;
		if (N == 0)break;
		if (N % 1111 == 0)printf("NA\n");
		else{
			ans = 0;
			while (N != 6174){
				int A[4];
				A[0] = N / 1000;
				A[1] = (N / 100) % 10;
				A[2] = (N / 10) % 10;
				A[3] = N % 10;
				sort(A, A + 4);
				reverse(A, A + 4);
				N = 999 * (A[0] - A[3]) + 90 * (A[1] - A[2]);
				ans++;
				
			}
			
			printf("%d\n", ans);
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int f(int p) {
	int a[10];
	a[0] = (p - p % 1000) / 1000;
	p -= a[0] * 1000;
	a[1] = (p - p % 100) / 100;
	p -= a[1] * 100;
	a[2] = (p - p % 10) / 10;
	p -= a[2] * 10;
	a[3] = p;
	sort(a, a + 4);
	return a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0] - a[0] * 1000 - a[1] * 100 - a[2] * 10 - a[3];
}
int main() {
	int n; while (cin >> n) {
		if (n == 0)return 0;
		else if (n % 1111 == 0) { cout << "NA\n"; continue; }
		else {
			for (int i = 0; i <= 6174; i++) {
				if (n == 6174) {
					cout << i << endl;
					i = 10000;
				}
				else n = f(n);
			}
		}
	}

}

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

int main() {
	int n;
	int a[4] = {};

	while (std::cin >> n) {
		if (n == 0000)
			break;

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

		int count = 0;
		while (n != 6174) {
			a[0] = n / 1000, n %= 1000;
			a[1] = n / 100, n %= 100;
			a[2] = n / 10, n %= 10;
			a[3] = n;

			std::sort(a, a + 4);

			n = 0;
			n += (a[3] - a[0]) * 1000;
			n += (a[2] - a[1]) * 100;
			n += (a[1] - a[2]) * 10;
			n += a[0] - a[3];

			++count;
		}

		std::cout << count << std::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) {
		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<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>

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

using namespace std;

int main(){
	for(char s[5];scanf("%s",s),strcmp(s,"0000")!=0;){
		if(count(s,s+4,s[0])==4){
			puts("NA");
			continue;
		}

		int ans=0;
		while(strcmp(s,"6174")!=0){
			char s1[5]; strcpy(s1,s); sort(s1,s1+4);
			char s2[5]; strcpy(s2,s); sort(s2,s2+4,greater<char>());
			int a1=atoi(s1);
			int a2=atoi(s2);
			sprintf(s,"%04d",a2-a1);
			ans++;
		}
		printf("%d\n",ans);
	}

	return 0;
}

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


int to_i(string &s){
	int a=0;
	for(int i=0;i<s.size();i++)a=a*10+s[i]-'0';
	return a;
}
string to_s(int n){
	string s;
	while(n){
		s+=n%10+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}



int cache[10000];

int solve(int n){
	if(n==6174)return 0;
	if(cache[n])return n;
	string s=to_s(n);
	int a,b;
	sort(s.begin(),s.end());
	a=to_i(s);
	sort(s.begin(),s.end(),greater<char>());
	b=to_i(s);
	return cache[n] = 1+solve(b-a);
}


int main(){
	string s;
	while(cin>>s&&s!="0000"){
		if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){
			cout<<"NA\n";
			continue;
		}
		cout<<solve(to_i(s))<<endl;
		
	}
	return 0;
}

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

using namespace std;

int main()
{
	string S;

	while (true)
	{
		cin >> S;

		if (S == "0000") { break; }

		if (S[0] == S[1] == S[2] == S[3])
		{
			cout << "NA" << endl;
		}
		else
		{
			int count_ = 0;

			while (S != "6174")
			{
				string S1 = S, S2 = S;

				sort(S1.begin(), S1.end(), greater<char>());
				sort(S2.begin(), S2.end());

				S = to_string(stoi(S1) - stoi(S2));

				while (S.size() < 4)
				{
					S = '0' + S;
				}

				count_++;
			}

			cout << count_ << endl;
		}
	}

	return 0;
}

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

int main() {
	string N;
	
	while (cin >> N) {
		if (N == "0000") break;
		int ok = 1, ans = 0;
		unordered_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();
			
			while (N.size() < 4) N = "0" + N;
			
			++ans;
			
			if (strs.find(N) != strs.end()) ok = 0;
		}
		
		if (ok) cout << ans << endl;
		else cout << "NA" << endl;
	}
}