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<iostream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<cstdlib>

int main() {
	char str[4];
	while (std::cin >> str) {
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == '0')
				break;
			else
				std::cout << "NA" << std::endl;
		}
		char l[4], s[4];
		int cnt = 0;
		while(str!="6174") {
			++cnt;
			std::memcpy(l, str, 4);
			std::memcpy(s, str, 4);
			std::sort(l, l + 4, std::greater<char>());
			std::sort(s, s + 4);
			int a = atoi(l) - atoi(s);
			itoa(a, str, 10);
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}

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