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 <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)

string N;

int solve(){
  string s = (N);
  int cou = 0;
  while(s != "6174"){
    sort(s.begin(),s.end());
    if(s.front() == s.back()) return -1;
    int S = atoi(s.c_str());
    reverse(s.begin(),s.end());
    int L = atoi(s.c_str());
    s = to_string(L - S);
    cou++;
  }

  return cou;
}
int main(){
  while(cin >> N,N != "0000"){
    int ans = solve();
    if(ans == -1){
      cout << "NA" << endl;
    }
    else{
      cout << ans << endl;
    }
  }
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
bool NA(string s) {
	if (s == "1111" || s == "2222" || s == "3333" || s == "4444" || s == "5555" || s == "6666" || s == "7777" || s == "8888" || s == "9999")return true;
	else return false;
}
signed main() {
	string s;
	while (cin >> s, s != "0000") {
		int ans(0);
		while (s != "6174") {
			if (NA(s)) {
				cout << "NA" << endl;
				goto here;
			}
			string big(s), small(s);
			sort(all(small));
			sort(all(big), greater<char>());
			s = to_string(stoi(big) - stoi(small));
			ans++;
		}
		cout << ans << endl;
	here:;

	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

map<string, int> mp;

int dfs(string curr) {
  if (mp.find(curr) != mp.end()) return mp[curr];
  if (curr == "6174") return 0;
  string L(curr), S(curr);
  sort(L.rbegin(), L.rend());
  sort(S.begin(), S.end());
  string next = to_string(stoi(L) - stoi(S));
  while (next.size() < 4) next = "0" + next;
  return mp[curr] = dfs(next) + 1;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  string N;
  while (cin >> N, N != "0000") {
    if (N[0] == N[1] && N[0] == N[2] && N[2] == N[3]) {
      cout << "NA" << endl;
      continue;
    }
    cout << dfs(N) << endl;
  }

  return 0;
}

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

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

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

int main(){
	string str;
	int n;
	while(cin>>str){
		if(str=="0000")return 0;
		stringstream ss(str);
		n =atoi(ss.str().c_str());
		//cout <<n<<endl;
		vector<int> L;
		vector<int> S;
		int cnt = 0;
		bool ok =true;
		while(n!=6174){
			cnt++;
			L.clear();
			S.clear();
			//cout <<n<<endl;
			while(n){
				int x;
				x =(n%10);
				n /=10;
				L.push_back(x);
			}
			S =L;
			sort(S.begin(),S.end());
			sort(L.begin(),L.end(),greater<int>());
			/*for(int i =0;i <L.size();i++){
				cout <<S[i]<<endl;
			}
			cout <<endl;
			for(int i =0;i <S.size();i++){
				cout <<L[i]<<endl;
			}
			cout <<endl;*/
			int c =4;
			int l,s;
			l = s=0;
			while(c--){
				l +=(L[3-c]*pow(10.0,c));
				s +=(S[3-c]*pow(10.0,c));
			}
			n =(l-s);
			//cout <<l<<" "<<s<<endl;
			/*if(cnt==3){
				return 0;
			}*/
				if(n==0){
					cout <<"NA"<<endl;
					ok =false;
					break;
				}
			}
			if(ok){
				cout <<cnt<<endl;
			}
		}
		return 0;
	}

C++: #include<iostream>
#include<sstream>
#include<algorithm>

using namespace std;


bool invalid(string s){
  char c = s[0];

  for(int i = 0; i < s.length(); i++)
    if(s[i] != c) return false;

  return true;
}

string tos(int n){
  string out = "";

  for(int i = 0; i < 4; i++){
    out += n%10+'0';
    n/=10;
  }
  reverse(out.begin(),out.end());
  return out;
}

string solve(string s){
  string A,B;

  sort(s.begin(),s.end());
  A = s;
  reverse(s.begin(),s.end());
  B = s;

  int a,b;
  stringstream s1(A),s2(B);

  s1 >> a;
  s2 >> b;
  return tos(b-a);
}

int main(){
  string s;
  while(cin >> s && s != "0000"){
    int ans = 0;

    if(invalid(s)){
      cout << "NA" << endl;
      continue;
    }

    while(s!="6174"){
      s = solve(s);
      ans++;
    }

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

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

int main(){
  int a[4];
  int n;
  
  while(1){
    cin>>n;
    if(n==0) break;
    int nmax,nmin;
    int co=0;
    while(1){
      if(n==0 || n==6174) break;
      co++;
      for(int i=0;i<4;i++){
	a[i] = n%10;
	n/=10;
      }
      nmax=0,nmin=0;
      sort(a,a+4);
      for(int i=0;i<4;i++){
	nmin = nmin*10+a[i];
	nmax = nmax*10+a[3-i];
      }
      n=nmax-nmin;
    }
    if(n==0) cout<<"NA"<<endl;
    else cout <<co<<endl;
  }


  return 0;
}

C++: #include<iostream>
#include<string>
#include<sstream>
#include<cmath>
#include<algorithm>
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<stdio.h>
#include<algorithm>
#include<functional>
using namespace std;
int main(void){
	int n,i,a,j;
	int de,syo,tem[4];
	while(1){
		scanf("%d",&n);
		if(n==0000)
			break;
		int cnt=0,flg=0;
		while(n!=6174){
			de=syo=0;
			for(i=0;i<4;i++){
				a=1;
				for(j=0;j<3-i;j++){
					a*=10;
				}
				tem[i]=n/a;
				n-=tem[i]*a;
			}
			if(tem[0]==tem[1]==tem[2]==tem[3]){
				flg=1;
				break;
			}
			sort(tem,tem+4);
			a=1000;
			for(i=0;i<4;i++){
				syo+=tem[i]*a;
				a/=10;
			}
			sort(tem,tem+4,greater<int>());
			a=1000;
			for(i=0;i<4;i++){
				de+=tem[i]*a;
				a/=10;
			}
			cnt++;
			n=de-syo;
		}
		if(flg==0)
			printf("%d\n",cnt);
		else 
			printf("NA\n");
	}
	return 0;
}