Go: package main

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

var memo map[int]int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if _, ok := memo[v]; !ok {
		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))
		memo[v] = solve(l-s) + 1
	}
	return memo[v]
}

func main() {
	var v int
	memo = make(map[int]int)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include <iostream>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <algorithm>

using namespace std;

void solve()
{
	string str;
	while(cin >> str)
	{
		if(str == "0000")
		{
			break;
		}
		else if(atoi(str.c_str()) % 1111 == 0)
		{
			cout << "NA" << endl;
			continue;
		}
		int count = 0;
		while(str != "6174")
		{
			sort(str.begin(), str.end());
			string small = str;
			string large;
			for(int i = str.size() - 1; i >= 0; --i)
			{
				large += str[i];
			}
			int num = atoi(large.c_str()) - atoi(small.c_str());
			stringstream ss;
			ss << num;
			str = ss.str();
			if(str.size() < 4)
			{
				string buffer = str;
				str.clear();
				for(int i = 0; i < 4 - buffer.size(); ++i)
				{
					str += "0";
				}
				str += buffer;
			}
			++count;
		}
		cout << count << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include<bits/stdc++.h>

using namespace std;

string N;


bool cmp(const char c1, const char c2) {
	return c1 > c2;
}

int solve() {
	int L, S, cnt;
	stringstream ss;

	cnt = 0;
	while (true) {
		if (N == "6174") {
			return cnt;
		}
		sort(N.begin(), N.end(), cmp);
		ss << N;
		ss >> L;
		//cout << L << " :output_L" << endl;
		ss.clear();
		sort(N.begin(), N.end());
		ss << N;
		ss >> S;
		//cout << S << " :output_S" << endl;
		ss.clear();
		ss << (L - S);
		ss >> N;
		ss.clear();
		while (N.size() < 4) {
			N = "0" + N;
		}
		//cout << "new:" << N << endl;
		++cnt;
	}
	
}

int main() {

	while (cin >> N, N != "0000") {
		if (N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
			cout << "NA" << endl;
			continue;
		}
		cout << solve() << endl;
	}
	
	return 0;
}

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

using namespace std;

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

typedef long long ll;

int main()
{
	string s;
	while(cin >> s)
	{
		if(s == "0000")
		{
			break;
		}
		int res = 0;
		if(s == "6174")
		{
			cout << res << endl;
			continue;
		}
		for(int i = 1; ;i++)
		{
			string UP = s, LOW = s;
			sort(LOW.begin(), LOW.end());
			sort(UP.begin(), UP.end(), greater<char>());

			int up = to_int(UP), low = to_int(LOW);
			int mid = up - low;

			if(mid == 0)
			{
				res = -1;
				break;
			}
			if(mid == 6174)
			{
				res = i;
				break;
			}
			s = to_str(mid);
		}

		if(res == -1)
		{
			cout << "NA" << endl;
			continue;
		}
		cout << res << endl;
	}

}

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

int main(){
	int num[4],l,s,aa=0;
	string n;
	while(cin>>n,n!="0000"){
		for(int i=0;i<4;i++)num[i]=n[i]-'0';
		aa=0;
		if(num[0]==num[1]&&num[1]==num[2]&&num[2]==num[3]){
			cout<<"NA"<<endl;
			continue;
		}
		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 <iostream>
#include <string>
#include <algorithm>
#include <deque>
using namespace std;
string L(string);
string S(string);
string sa(string,string);
int main(void)
{
	int count;
	string n;

	while(1){
		cin >> n;
		if(n=="0000") break;
		if(n=="1111"||n=="2222"||n=="3333"||n=="4444"||n=="5555"||n=="6666"||n=="7777"||n=="8888"||n=="9999") {
			cout << "NA" << endl;
			continue;
		}
		count=0;
		while(n!="6174"){
			n=sa(L(n),S(n));
			count+=1;
		}
		cout << count << endl;
	}
	return 0;
}
string S(string n)
{
	string s;
	deque<char> dn;
	for(int i=0;i<4;i++) dn.push_back(n[i]);
	sort(dn.begin(),dn.end());
	for(int i=3;0<=i;i--){
		s+=dn[i];
		dn.pop_back();
	}
	return s;
}
string L(string n)
{
	string l;
	deque<char> dn;
	for(int i=0;i<4;i++) dn.push_back(n[i]);
	sort(dn.begin(),dn.end());
	for(int i=0;i<4;i++){
		l+=dn[i];
	}
	return l;
}
string sa(string l,string s)
{
	string n;
	int lnum=0,snum=0,num,b=1;
	for(int i=3;0<=i;i--){
		lnum+=(b*(l[3-i]-'0'));
		snum+=(b*(s[3-i]-'0'));
		b*=10;
	}
	num=lnum-snum;
	for(int i=0;i<4;i++){
		b/=10;
		n+=((num/b)%10+'0');
	}
	cout << n << endl;
	return n;
}

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

int main() {
	int n;
	bool used[10000];
	while (std::cin >> n) {
		int str[4] = { (n / 1000),(n / 100) % 10,(n / 10) % 10,n % 10 };
		if (n % 1111 == 0) {
			if (str[0] == 0)
				break;
			else
				std::cout << "NA" << std::endl;

		}
		std::memset(used, false, sizeof(used));
		used[n] = true;
		int cnt = 0;
		while(n!=6174) {
			++cnt;
			std::sort(str, str + 4, std::greater<int>());
			int l = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
			std::sort(str, str + 4);
			int	s = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
			n = l - s;
			if (used[n])
				goto a;
			used[n] = true;
			str[0] = n / 1000;
			str[1] = (n / 100) % 10;
			str[2] = (n / 10) % 10;
			str[3] = n % 10;
		}
		std::cout << cnt << std::endl;
	a:;
	}
	return 0;
}

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef set<int> si;
typedef set<ll, ll> sll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef map<int, int> mii;
typedef map<int, ll> mil;
typedef map<ll, int> mli;
typedef map<ll, ll> mll;

#define FOR(i, a, b) for(int i = (a), loop_end_##i=(b); i < (loop_end_##i); i++)
#define REP(i, n) FOR(i, 0, n)
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define SORT(c) sort((c).begin(),(c).end())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define PB push_back

#define dump(x)  cout << #x << " = " << (x) << endl
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl

#define rep(v,n) for(int v=0;v<n;v++)
#define input(t,v) t v;cin>>v;
#define input2(t,v1,v2) t v1,v2;cin>>v1>>v2;
#define input3(t,v1,v2,v3) t v1,v2,v3;cin>>v1>>v2>>v3;

template<class T> T read() {
	T t;
	cin >> t;
	return t;
}

int main(){
	while(true){
		input(int,n);
		if(n==0)return 0;
		if(n%1111==0){cout<<"NA"<<endl;continue;}
		int ret=0;
		while(n!=6174){
			int ds[4],tmp;
			tmp=n;
			rep(i,4){
				ds[i]=tmp%10;
				tmp/=10;
			}
			sort(begin(ds),end(ds));
			
			int S=ds[0]*1000+ds[1]*100+ds[2]*10+ds[3];
			int L=ds[3]*1000+ds[2]*100+ds[1]*10+ds[0];
			n=L-S;
			ret++;
		}
		cout<<ret<<endl;
	}
	
}