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<string>
#include<sstream>
#include<cmath>
using namespace std;


bool allsame(int n){
  int tmp=n%10;
  n/=10;
  while(n!=0){
    if(tmp!=n%10){
      return 0;
    }
    tmp=n%10;
    n/=10;
  }
  return 1;
}

string toSortString(int n){
  stringstream ss;
  ss<<n;
  string tmp;
  ss>>tmp;
  sort(tmp.begin(),tmp.end());
  return tmp;
}

int make_l(string s){
  int res=0;
  for(int i=0;i<s.size();i++){
    res+=(s[i]-'0')*pow(double(10),i);
  }
  return res;
}

int make_s(string s){
  int res=0;
  for(int i=0;i<s.size();i++){
    res+=(s[s.size()-1-i]-'0')*pow(double(10),i);
  }
  return res;
}

int main(){
  int n;
  int ans=0;
  while(cin>>n,n!=0){
    if(allsame(n)){
      cout<<"NA"<<endl;
      continue;
    }
    while(n!=6174){
      ans++;
      int l,s;
      string tmp_n=toSortString(n);
      l=make_l(tmp_n);
      s=make_s(tmp_n);
      n=l-s;
    }
    if(n==6174){
      cout<<ans<<endl;
    }
  }
}

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

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

string s;
string table[] = { "1111", "2222", "3333", "4444",
"5555", "6666", "7777", "8888", "9999" };
int changei(string s){
	stringstream ss;
	int res;
	ss << s;
	ss >> res;
	return res;
}

int main(){
	while (cin >> s){
		if (s == "0000")break;
		bool f = 0;
		REP(i, 9){
			if (s == table[i])f = 1;
		}
		if (f){
			cout << "NA" << endl;
			continue;
		}
		int ans = 0;
		while (s != "6174"){
			ans++;
			string t, u;
			sort(ALL(s));
			t = s;
			sort(ALL(s), greater<int>());
			u = s;
			int k = changei(u) - changei(t);
			stringstream ss;
			ss << k;
			ss >> s;
			reverse(ALL(s));
			while (s.size() < 4){
				s += '0';
			}
			reverse(ALL(s));
		}
		cout << ans << endl;
	}
}

C++: #include<iostream>
#include<string>
#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;
	}
	while(s.size()<4)s+='0';
	reverse(s.begin(),s.end());
	return s;
}


int cache[10000];

int solve(int n){
	if(n==6174)return 0;
	if(cache[n])return cache[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 <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

int main(){
	while(1){
		int n, x[4], y[4], count=0;
		bool flag = false;
		
		cin >> n;
		if(n==0) break;
	
		while(n!=6174){
			int a=0, b=0;
			//printf("%d\n", n);
			
			for(int i=0; i<4; ++i){
				x[i] = (n%(int)pow(10, 4-i) - n%(int)pow(10, 3-i)) / (int)pow(10, 3-i);
				y[i] = (n%(int)pow(10, 4-i) - n%(int)pow(10, 3-i)) / (int)pow(10, 3-i);
			}
			if(x[0]==x[1] && x[1]==x[2] && x[2]==x[3]){
				flag=true;
				break;
			}	
		
			sort(x, x+4);
			for(int i=0; i<4; ++i)
				a+=pow(10,3-i)*x[i];
			
			sort(y, y+4, greater<int>());
			for(int i=0; i<4; ++i)
				b+=pow(10,3-i)*y[i];
	
			n=b-a;
			
			++count;
		}
	
		if(flag) printf("NA\n");
		else printf("%d\n", count);
	}
	
	return 0;
}

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

int main()
{
  while (true) {
    string n;
    bool f = false;
    int cnt = 0;
    cin >> n;

    if (n == "0000") {
      break;
    }
    for (int i = 1; i < 4; i++) {
      if (n[0] != n[i]) f = true;
    }
    if (!f) {
      cout << "NA" << endl;
      continue;
    };

    while (n != "6174") {
      vector<char> a, b;
      for (int i = 0; i < 4; i++) {
        a.push_back(n[i]);
      }
      b = a;

      sort(a.begin(), a.end());
      sort(b.begin(), b.end(), greater<char>());

      string hayami = "";
      string kanade = "";
      for (int i = 0; i < 4; i++) {
        hayami += a[i];
        kanade += b[i];
      }

      int tmp_a, tmp_b;
      sscanf(hayami.c_str(), "%d", &tmp_a);
      sscanf(kanade.c_str(), "%d", &tmp_b);

      n = to_string(tmp_b - tmp_a);

      cnt++;
    }
    
    cout << cnt << endl;
  }
}

C++: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <functional>

using namespace std;

bool input(int a[]){
	int i, inp;
	scanf("%d",&inp);
	if(inp == 0)
		return false;
	for(i = 3;i >= 0;i--){
		a[i] = inp % 10;
		inp /= 10;
	}
	return true;
}
int main(void){
	int a[4], b[4], i, count;
	const int suc[4] = {6,1,7,4};
	while(input(a)){
		count = 0;
		while(1){
			for(i = 0;i < 4;i++)
				if(suc[i] != a[i]) break;
			if(i == 4){
				printf("%d\n",count);
				break;
			}
			for(i = 1;i < 4;i++)
				if(a[0] != a[i]) break;
			if(i == 4){
				printf("NA\n");
				break;
			}
			for(i = 0;i < 4;i++)
				b[i] = a[i];
			sort(a,a + 4,greater<int>());
			sort(b,b + 4);
			for(i = 3;i >= 0;i--){
				a[i] -= b[i];
				if(a[i] < 0){
					a[i - 1]--;
					a[i] += 10;
				}
			}
			count++;
		}
	}
	return 0;
}

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

int main(){
	for(int N; cin>>N && N;){
		for(int i=0;; i++){
			if(N==6174){
				cout<<i<<endl;
				break;
			}
			int c[] = {N/1000, (N/100)%10, (N/10)%10, N%10};
			sort(c, c+4);
			int S = 1000*c[0] + 100*c[1] + 10*c[2] + c[3];
			int L = 1000*c[3] + 100*c[2] + 10*c[1] + c[0];
			if(S==L){
				cout<<"NA"<<endl;
				break;
			}
			N = L-S;
		}
	}
}

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