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<bits/stdc++.h>
#define LIM 1+2+4+8+16+32+64+128
using namespace std;
typedef long long int ll;
typedef struct{
  int per;
  int ko;
  int kyoda;
  int konoko;
}TRE;
int ace(int a,int b){
  int ss=0;
  for(int i=b;i<=a;i++){
    ss+=i;
  }
  return(ss);
}
typedef pair<int,int>P;
int main(){
  int n;
  while(1){
    cin>>n;
    if(n==0){
      break;
    }
    if(n%1111==0){
      cout<<"NA"<<endl;
    }else{
      int cnt=0;
      while(n!=6174){
	cnt++;
	int l=0;
	int s=0;
	int sl[4];
	int ss=0;
	for(int i=10;i<=1000;i*=10){
	  sl[ss]=(n-((n/i)*i))/(i/10);
	  ss++;
	}
	sl[ss]=n/1000;
	sort(sl,sl+4);
	ss=0;
	for(int i=1;i<=1000;i*=10){
	  l+=sl[4-ss-1]*i;
	  s+=sl[ss]*i;
	  ss++;
	}
	n=abs(l-s);
      }
      cout<<cnt<<endl;
    }
  }
  return(0);
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void){
	string S;
	while(cin >> S){
		int cost = 0;
		string str = S;
		if(S == "6174"){
			cout << cost << endl;
			continue;
		}
		if(S == "0000")break;
		if(S[0] == S[1] && S[2] == S[3] && S[0] == S[3]){
			cout << "NA" << endl;
			continue;
		}
		while(1){
			cost++;
			vector<int> v;
			rep(i,4){
				int a = str[i]-'0';
				v.push_back(a);
			}
			sort(v.begin(), v.end(), greater<int>());
			int L = v[0] * 1000 + v[1] * 100 + v[2] * 10 + v[3];
			sort(v.begin(), v.end());
			int S = v[0] * 1000 + v[1] * 100 + v[2] * 10 + v[3];
			int sum = L - S;
			if(sum == 6174){
				cout << cost << endl;
				break;
			}
			str = IntToStr(sum);
		}


	}
	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

C++: #include<stdio.h>
int main(void)
{
	int n;
	int l,s;
	int i,j,temp;
	int keta[4];
	int cnt;
	
	scanf("%d",&n);
	
	while(n!=0000){
		if(n%1111==0){
			printf("NA\n");
		return 0;
		}
		cnt=0;
		while(n!=6174){
			for(i=0;i<4;i++){
				keta[i]=n%10;
				n=n/10;
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]<keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			l=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];

			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]>keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			s=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];
			n=l-s;
			cnt++;
		}
	printf("%d\n",cnt);
	scanf("%d",&n);
	}
	return 0;
}

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

using namespace std;

int main()
{
	int N[4], L[4], S[4], l, s, i, num;
	char input[5];
	
	while (1){
		scanf("%s", input);
		
		for (i = 0; i < 4; i++){
			N[i] = input[i] - '0';
		}
		
		if (input[0] == '0' && input[1] == '0' && input[2] == '0' && input[3] == '0'){
			break;
		}
		
		if (input[0] == input[1] && input[0] == input[2] && input[0] == input[3]){
			printf("NA\n");
			continue;
		}
		
		num = 0;
		while (1){
			if (N[0] == 6 && N[1] == 1 && N[2] == 7 && N[3] == 4){
				break;
			}
			
			for (i = 0; i < 4; i++){
				L[i] = N[i];
				S[i] = N[i];
			}
			
			sort(L, L + 4);
			reverse(L, L + 4);
			sort(S, S + 4);

			for (i = 3; i >= 0; i--){
				N[i] = L[i] - S[i];
				if (N[i] < 0){
					N[i] += 10;
					L[i - 1]--;
				}
			}
			
			num++;
		}
		
		printf("%d\n", num);
	}
	
	return (0);
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <iostream>

using namespace std;

int main(){

	while(1){
		int n=0,l,s;
		int cnt = 0;
		string tmp;

		cin >> tmp;
		n = atoi(tmp.c_str());

		if(n == 0)break;
		if(n % 1111 == 0){
			printf("NA\n");
			continue;
		}

		while(n != 6174){
			sort(tmp.begin(),tmp.end());
			s = atoi(tmp.c_str());

			sort(tmp.begin(),tmp.end(),greater<char>());
			l = atoi(tmp.c_str());
			n = l - s;

			for(int i=0,p=1000;i<4;i++,p/=10){
				tmp[i] = '0' + n/p;
				n %= p;
			}
			n = atoi(tmp.c_str());
			cnt++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}

C++: #include <bits/stdc++.h>
typedef long long lint;
using namespace std;
#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)

int f(string n) {
    int cnt = 0;
    while (n != "6174") {
        sort(n.begin(),n.end());
        int l = stoi(n);
        sort(n.begin(),n.end(), greater<int>());
        int s = stoi(n);
        if (s == l) {
            return -1;
        }
        ostringstream str;
        str << setfill('0') << setw(4) << s-l;
        n = str.str();
        cnt ++;
    }
    return cnt;
}

bool solve() {
    string n; cin >> n;
    if (n == "0000") return false;
    int cnt = f(n);
    if (cnt == -1) cout << "NA" << endl;
    else cout << cnt << endl;

    return true;
}

int main() {
    while (solve()) {}
}

C++: // 2014/09/05 Tazoe

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

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

		if(N==0)
			break;

		if(N%1111==0){		// 全桁が同じ数字
			cout << "NA" << endl;
			continue;
		}

		int cnt = 0;

		while(N!=6174){
			int n[4];
			n[0] = N/1000;
			n[1] = N/100%10;
			n[2] = N/10%10;
			n[3] = 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 <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, s != "0000") {
		int ans = 0;
		if (toInt(s) % 1111 == 0) {
			cout << "NA" << endl;
		}
		else {
			while (s != "6174") {
				string a = s, b = s;
				sort(ALL(a));
				sort(RALL(b));
				s = toStr(toInt(b) - toInt(a));
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}