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.Print("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<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

#define pb push_back

int memo[10010];
int cnt(int n){
	vector<int> vec;
	vec.pb(n%10);
	vec.pb((n/10)%10);
	vec.pb((n/100)%10);
	vec.pb(n/1000);
	sort(vec.begin(),vec.end());
	int l = 0 , s = 0;
	for(int i = 0 ; i < 4 ; i ++){
		l *= 10;
		l += vec[vec.size()-1-i];
	}
	for(int i = 0 ; i < 4 ; i ++){
		s *= 10;
		s += vec[i];
	}
	return l-s;
}
int dfs(int n){
	if(memo[n] == -1)return memo[n] = dfs(cnt(n))+1;
	else return memo[n];
}

int main(){
	for(int i = 0 ; i < 10010 ; i ++){
		memo[i] = -1;
	}
	memo[6174] = 0;
	while(1){
		int n;
		scanf("%d",&n);
		if(n == 0)break;
		if(n%1111 == 0){
			puts("NA");
			continue;
		}
		printf("%d\n",dfs(n));
	}
}

C++: #include "bits/stdc++.h"
// Custom Header {{{
#define ALL(x) x.begin(), x.end()
#define endl '\n'
using namespace std;
using i64 = long long;
using pii = pair<i64, i64>;
template<class A, class B>inline bool chmax(A &a, const B &b){return b>a ? a=b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, const B &b){return b<a ? a=b,1 : 0;}
constexpr int INF  = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;
// }}}


signed main()
{
    //cin.tie(nullptr); ios::sync_with_stdio(false);

    string s;
    char buf[5];
    while(cin >> s, s != "0000") {
        int ans = 0;

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

        while(s != "6174") {
            string L = s;
            string S = s;
            sort(ALL(L), greater<>());
            sort(ALL(S));
            sprintf(buf, "%04d", stoi(L) - stoi(S));
            s = buf;
            ++ans;
        }

        cout << ans << endl;
    }
    return 0;
}

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

int stoi(string s){
  stringstream ss(s);
  int res; ss >> res;
  return res;
}

int main(){
  string s;
  while(cin >> s,s!="0000"){
    bool f = true;
    for(int i=0;i<3;i++)f &= (s[i]==s[i+1]);
    if(f){ cout << "NA\n"; continue; }

    int cnt = 0;
    while(s!="6174"){
      sort(s.begin(),s.end());
      int S = stoi(s);
      reverse(s.begin(),s.end());
      int L = stoi(s);
      char* N; sprintf(N,"%04d",L-S);
      for(int i=0;i<4;i++)s[i] = N[i];
      cnt++;
    }
    cout << cnt << endl;
  }
}

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

string cn;
int main()
{
  while(true){
    cin >> cn;
    if(cn == "0000"){
      break;
    }
    if(cn[0] == cn[1] && cn[0] == cn[2] && cn[0] == cn[3]){
      cout << "NA" << endl;
    }

    else{
      int ans = 0;
      while(cn != "6174"){
	int in, intss;
	stringstream ss1, ss2, ss3;
	string cncp;
	sort(cn.begin(), cn.end(), greater<char>());
	ss1 << cn;
	ss1 >> intss;
	in = intss;
	for(int j = 0; j < 4; j++){
	  cncp += cn[3 - j];
	}
	ss2 << cncp;
	ss2 >> intss;
	in -= intss;
	ss3 << in;
	ss3 >> cn;
	ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

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

int N;
int Ns, Nl;
int num[4];
int S[4], L[4];
int result;

int main() {
    while (true) {
        result = 0;
        cin >> N;
        
        if (!N) break;            
        
        if (N != 6174) {
            num[0] = N / 1000;
            N %= 1000;
            num[1] = N / 100;
            N %= 100;
            num[2] = N / 10;
            N %= 10;
            num[3] = N;
            
            while (true) {
                Ns = 0; Nl = 0;
                int judge = 0;
                for (int i = 0; i < 4; i++) {
                    if (num[0] == num[i]) judge++;
                }

                if (judge == 4) {
                    cout << "NA" << endl;
                    break;
                }

                sort(num, num + 4);
                for (int i = 0; i < 4; i++) {
                    S[i] = num[i];
                }

                Ns += S[0] * 1000;
                Ns += S[1] * 100;
                Ns += S[2] * 10;
                Ns += S[3];

                sort(num, num + 4, greater<int>());
                for (int i = 0; i < 4; i++) {
                    L[i] = num[i];
                }

                Nl += L[0] * 1000;
                Nl += L[1] * 100;
                Nl += L[2] * 10;
                Nl += L[3];

                N = Nl - Ns;

                num[0] = N / 1000;
                N %= 1000;
                num[1] = N / 100;
                N %= 100;
                num[2] = N / 10;
                N %= 10;
                num[3] = N;

                result++;

                if(num[0] == 6 && num[1] == 1 && num[2] == 7 && num[3] == 4) {
                    cout << result << endl;
                    break;
                }
            }
        }
        else cout << 0 << endl;
        
    }
    
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
string s;
int main() {
	while(cin >> s, s != "0000") {
		if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) {
			cout << "NA" << endl;
			continue;
		}
		for(int i = 0; ; i++) {
			if(s == "6174") {
				cout << i << endl;
				break;
			}
			sort(s.begin(), s.end());
			string t = s;
			reverse(t.begin(), t.end());
			//cout << s << ' ' << t << endl;
			s = to_string(abs(stoi(s) - stoi(t)));
			while(s.size() < 4) s = "0" + s;
		}
	}
}

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

int dp[10010];

int solver(int x){
	if(dp[x]!=-1)return dp[x];
	vector<int> a;
	for(int i=0;i<4;i++){
		a.push_back(x%10);
		x/=10;
	}
	sort(a.begin(),a.end());

	int L=0,S=0;
	for(int i=3;i>=0;i--){
		L*=10;
		L+=a[i];
	}
	for(int i=0;i<4;i++){
		S*=10;
		S+=a[i];
	}

	return dp[x]=solver(L-S)+1;
}
		
int main(){
	for(int i=0;i<10010;i++)dp[i]=-1;
	dp[6174]=0;
	int N;
	while(1){
		cin>>N;
		if(N==0)break;

		bool c=true;

		for(int i=1;i<=9;i++){
			if(N==1111*i){printf("NA\n"); c=false; }
		}

		if(c)printf("%d\n",solver(N));
	}
}

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

typedef long int lint;
typedef long long int llint;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
typedef pair<long, long> pll;

#define EACH(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, e) for (int i = (s); i < (int)(e); ++i)
#define REP(i, n) FOR(i, 0, n)
#define endl '\n'
#define mod 1000000007
#define INF 100000000
#define fk first
#define sv second

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1};

template<typename T>
void print(const std::vector<T>& v, char sep=' ') {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i != 0) cout << sep;
    cout << v[i];
  }
  cout << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;

  while (cin >> n){
    int ct = 0;
    if (n % 1111 == 0){
      cout << "NA\n";
      continue;
    }
    
    while (n != 6174){
      int l, s, num[4];
      REP(i, 4){
        num[i] = n % 10;
        n /= 10;
      }
      sort(num, num + 4);
      l = s = 0;
      REP(i, 4){
        l = l * 10 + num[3 - i];
        s = s * 10 + num[i];
      }
      n = l - s;
      ct++;
    }
    cout << ct << endl;
  }
}