Go: package main

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

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

func main() {
	var v int
	memo = make([]int, 10000)
	for {
		var s string
		fmt.Scan(&s)
		v, _ = strconv.Atoi(s)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

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-12;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

int main() {
	string s;
	while(cin >> s, s!= "0000") {
		int tmp = toInt(s);
		if(tmp == 6174) {
			cout << 0 << endl;
			goto next;
		} else if(tmp%1111 == 0) {
			cout << "NA" << endl;
			goto next;
		}
		
		int count = 1;
		while(true) {	
			sort(ALL(s));
			int a = toInt(s);
			sort(RALL(s));
			int b = toInt(s);
			int c = b - a;
			if(c == 6174) {
				break;
			} else {
				s = toStr(c);
				count++;
			}
		}
		cout << count << endl;

		next:;
	}

	return 0;
}

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);
				s.resize(4, '0');
				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<iostream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<cstdlib>

int main() {
	int n;
	int str[4];
	while (std::cin >> n) {
		int str[4] = { (n / 1000),(n / 100) % 10,(n / 10) % 10,n % 10 };
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == 0)
				break;
			else
				std::cout << "NA" << std::endl;
		}
		int cnt = 0;
		while(n!=6174) {
			if (str[0] == str[1] == str[2] == str[3])
				std::cout << "NA" << std::endl;
			++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;
			str[0] = n / 1000;
			str[1] = (n / 100) % 10;
			str[2] = (n / 10) % 10;
			str[3] = n % 10;
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}

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

		if(N==0000)
			break;

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

		while(N!=6174){
			int n[4]={N/1000,N/100%10,N/10%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<cstdio>
#include<cstring>
#include<functional>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
int main(void)
{
	int i,j;
	int n,cnt,l,s,flg;
	int a[3];
	while(1) {
		cin >> n;
		if(n==0000) break;
		cnt=0;
		flg=0;
		while(1) {
			if(n==6174) break;
			a[0]=n/1000;
			n=n%1000;
			a[1]=n/100;
			n=n%100;
			a[2]=n/10;
			a[3]=n%10;
			
			if(a[0]==a[1] && a[0]==a[2] && a[0]==a[3]) {cout << "NA" << endl;flg=1;break;}
			sort(a,a+4);
	//		cout << a[0] << a[1] << a[2] << a[3] << endl;
			s=(a[0]*1000)+(a[1]*100)+(a[2]*10)+a[3];
			sort(a,a+4,greater<int>());
			l=(a[0]*1000)+(a[1]*100)+(a[2]*10)+a[3];
	//		cout << a[0] << a[1] << a[2] << a[3] << "==" << l << endl;
			if(l-s==6174) {cnt++;break;}
			else {n=l-s;cnt++;}
		}
		if(flg==0)cout << cnt << endl;
	}
	exit(0);
}

C++: // 2013/06/14 Tazoe

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	while(true){
		string N;
		cin >> N;

		if(N=="0000")
			break;

		if(N[0]==N[1] && N[1]==N[2] && N[2]==N[3]){
			cout << "NA" << endl;
			continue;
		}

		int cnt = 0;

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

		while(true){
			if(n==6174){
				cout << cnt << endl;
				break;
			}

			int tmp[4];
			tmp[0] = n/1000;
			tmp[1] = n/100%10;
			tmp[2] = n/10%10;
			tmp[3] = n%10;

			sort(tmp, tmp+4);
			int L = tmp[3]*1000+tmp[2]*100+tmp[1]*10+tmp[0];
			int S = tmp[0]*1000+tmp[1]*100+tmp[2]*10+tmp[3];

			n = L-S;
			cnt++;
		}
	}

	return 0;
}

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

int func(string n){
    if(n[0]==n[1]&&n[1]==n[2]&&n[2]==n[3])return -1;
    
    string L,S;
    
    for (int cnt=0; ; cnt++) {
        if(n=="6174")return cnt;
        L=S=n;
        sort(L.begin(), L.end(), greater<char>());
        sort(S.begin(), S.end());
        int x=atoi(L.c_str())-atoi(S.c_str());
        for (int i=0,p=1000; i<4; i++,p/=10) {
            n[i]='0'+x/p;
            x%=p;
        }
    }
}

int main() {
    while (1) {
        string N;
        cin>>N;
        if(N=="0000")break;
        int ans=func(N);
        if(ans==-1){
            cout<<"NA"<<endl;
        }else{
            cout<<ans<<endl;
        }
    }
}

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

int main(void){
  int N;
  vector<int> nums;
  
  while(cin >> N){
    int times=0;
    if(N==0){
      break;
    }
    if(N%1111==0){
      cout << "NA" << endl;
    }
    else{
      while(N!=6174){
	for(int i=1;i<=4;i++)
	  nums.push_back( (N% (int)pow(10,i) )/(int)pow(10,i-1) );
	N=0;
	sort(nums.begin(),nums.end(),greater<int>());
	for(int i=0;i<4;i++)
	  N+=nums[i]*pow(10,4-i-1)-nums[i]*pow(10,i);
	times++;
	nums.clear();
      } 
      cout << times << endl;
    }
  }
  return 0;
}