Go: package main

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

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		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([]int, 10000)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int operate(int n)
{
  vector<int> v(4);
  for(int i = 0; i<4; i++){
    v[i] = n % 10;
    n /= 10;
  }
  int L = 0,S = 0;
  sort(v.begin(),v.end());
  for(int i=0; i<4; i++){
    L *= 10; L += v[3-i];
    S *= 10; S += v[i];
  }
  return L - S;
}
int main()
{
  for(;;){
    int n;
    cin >> n;
    if(n == 0) break;
    if(n == 6174){
      cout << 0 << endl;
      continue;
    }
    if(n % 1111 == 0){
      cout << "NA" << endl;
      continue;
    }
    int ret = 1;
    for(;;){
      n = operate(n);
      if(n == 6174) break;
      ret++;
    }
    cout << ret << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
int stoi(string a){
		int b;
		stringstream ss;
		ss<<a;
		ss>>b;
		return b;
	}
	string itos(int a){
		stringstream ss;
		ss <<a;
		string b;
		b=ss.str();
		return b;
	}

int main(){
	string n;int t;
	while(cin>>n,n!="0000"){
		for(int i=0;i<4-n.size();i++)n="0"+n;
		int ans=0;
		bool flag=true;
		if(n[0]==n[1]&&n[1]==n[2]&&n[2]==n[3])flag=false;
		
		if(flag){
			while(n!="6174"){
				ans++;
				int a[4]={0},b[4]={0},ai,bi;
				for(int i=0;i<4;i++)a[i]=n[i]-'0',b[i]=n[i]-'0';
				sort(a,a+4);
				sort(b,b+4);
				reverse(b,b+4);
				int tmp=0;
				tmp=1000*b[0]+100*b[1]+10*b[2]+b[3];
				tmp-=1000*a[0]+100*a[1]+10*a[2]+a[3];
				n=itos(tmp);
				//cout<<n<<endl;
			}
		}
		if(flag)cout<<ans<<endl;
		else cout<<"NA"<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<b?(a=b,1):0)
#define chmin(a,b) (a>b?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

int main() {
  string n;
  while(cin>>n, n!="0000") {
    if (n[0]==n[1]&&n[0]==n[2]&&n[0]==n[3]) {
      puts("NA");
      continue;
    }
    int res = 0;
    while(1) {
      if (n == "6174") break;
      string tmp = n;
      sort(ALL(tmp),greater<int>());
      int L = atoi(tmp.c_str());
      sort(ALL(tmp));
      int S = atoi(tmp.c_str());
      int nxt = L-S;
      char hoge[10];
      sprintf(hoge,"%04d",nxt);
      n = string(hoge);
      // cout << n << endl;
      res++;
    }
    cout << res << 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;
	}
	reverse(s.begin(),s.end());
	return s;
}


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;
		}
		int ans=0;;
		while(s!="6174"){
			ans++;
			int a,b;
			sort(s.begin(),s.end());
			a=to_i(s);
			sort(s.begin(),s.end(),greater<char>());
			b=to_i(s);
			s=to_s(b-a);
		}
		cout<<ans<<endl;
		
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int n;
	int ans;
	int d[4];
	int tmp,big,sma;
	
	while(cin>>n,n){
		if(n%1111==0)cout<<"NA"<<endl;
		else{
			ans=0;
			while(n!=6174){
				tmp=n;
				sma=big=0;
				for(int i=0;i<4;i++){
					d[i]=tmp%10;
					tmp/=10;
				}
				sort(d,d+4);
				for(int i=0;i<4;i++){
					sma*=10;
					sma+=d[i];
				}
				for(int i=3;i>=0;i--){
					big*=10;
					big+=d[i];
				}
				n=big-sma;
				ans++;
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <sstream>

#define rep(i, j) FOR(i, 0, j)
#define FOR(i, j, k) for(int i = j; i < k; ++i)

using namespace std;

int main(){
  char n[5];
  while(scanf("%s", n) && strcmp(n, "0000") != 0){
    if(n[0] == n[1] && n[1] == n[2] && n[2] == n[3]){ puts("NA"); continue;}
    int c = 0;
    while(strcmp(n, "6174") != 0){
      rep(i, 4) FOR(j, i + 1, 4){
	if(n[i] > n[j]){ char tmp = n[i]; n[i] = n[j]; n[j] = tmp;}
      }
      int a = atoi(n);
      char tmp = n[0]; n[0] = n[3]; n[3] = tmp;
      tmp = n[1]; n[1] = n[2]; n[2] = tmp;
      int b = atoi(n);
      b -=  a;
      sprintf(n, "%d", b);
      c++;
    }
    printf("%d\n", c);
  }
  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) {
		if (s == "0000") {
			break;
		}
		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;
}

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

int main () {
    string str;
    while (cin >> str) {
        bool f = false;
        for (auto i : str) if (i != '0') f = true;

        if (!f) break;

        f = false;
        for (auto i : str) if (i != str[0]) f = true;

        if (!f) {
            cout << "NA" << endl;
            continue;
        }

        int count = 0;
        int now;
        stringstream org;
        org << str;
        org >> now;
        while (1) {
            if (now == 6174) {
                cout << count << endl;
                break;
            }
            count++;
            sort(str.begin(), str.end());
            auto rstr = str;
            reverse(rstr.begin(), rstr.end());

            stringstream ss;
            ss << rstr << " " << str;
            int A, B;
            ss >> A >> B;
            now = A - B;
            stringstream next;
            next << now;
            next >> str;
            while (str.size() < 4) str = "0" + str;
        }
    }

    return 0;
}