Go: package main

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

var memo map[int]int

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

C++: #include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include <strstream>
#include<algorithm>
#include<cstdio>

#include<functional>

using namespace std;


string IntToString(int number)
{
stringstream ss;
ss << number;
return ss.str();
}

int main(){
	string s;
	while(cin>>s)
	{
		if(s=="0000")break;
		int count=0;
		while (1)
		{
			
			if(s=="6174"){
				puts("0");
				break;
			}
			
			vector<int>n;
			
			for(int i=0; i<4; i++)
			{
				
				n.push_back( (int)s[i]-48 );
			}
			
			sort( n.begin(),n.end(),greater<int>() );
			int L=0,S=0;

			if( (n[0]+n[1]) - (n[2]+n[3]) == 0){
				puts("NA");
				break; 
			}
			
			L += n[0]*1000;
			L += n[1]*100;
			L += n[2]*10;
			L += n[3];
			
			reverse(n.begin(),n.end());
			
			S += n[0]*1000;
			S += n[1]*100;
			S += n[2]*10;
			S += n[3];
			
			s.clear();
			s = IntToString(L-S);
			count++;
			
			if(L-S==6174){
				cout<<count<<endl;
				break;
			}
		}

	}
	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://vjudge.net/contest/241120#problem/F>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */
string solve(string S){
    string res;
    for(int i = 0; i < 10;i++){
        if(count(S.begin(),S.end(),'0'+i) == 4) return "NA";
    }
    ll cnt = 0;
    while(true){
        while(S.length() < 4){
            S = "0" + S;
        }
        if(S == "6174") break;
        string L = S;
        sort(L.rbegin(),L.rend());
        sort(S.begin(),S.end());
        S = to_string(atoll(L.c_str())-atoll(S.c_str()));
        cnt++;
    }
    res = to_string(cnt);
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    string S;
    while(cin >> S){
        if(S == "0000") break;
        cout << solve(S) << endl;
    }
    return 0;
}

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

string toString(int n){
  string ret = "";
  while(n){
    ret += (n % 10) + '0';
    n /= 10;
  }
  reverse(ret.begin(), ret.end());
  return ret;
}

int main(){
  string s;

  while(cin >> s){
    if(s == "0000") break;    
    
    if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]){
      cout << "NA" << endl;
      continue;
    }
    
    int cnt = 0;
    while(s != "6174"){
      string L = s, S = s;
      sort(L.rbegin(), L.rend());
      sort(S.begin(), S.end());
      int nex = atoi(L.c_str()) - atoi(S.c_str());
      s = toString(nex);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

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

int main()
{
    string N;
    while ( cin >> N, N != "0000" ) {
        int cnt = 0;
        set < string > st;

        while ( N != "6174" && st.find(N) == st.end() ) {
            string L = N, S = N;
            int val = 0;
            st.insert(N);
            sort(rbegin(L), rend(L));
            sort(begin(S), end(S));
            val = atoi(L.c_str()) - atoi(S.c_str());
            N = to_string(val);
            while ( N.size() < 4 ) {
                N = '0' + N;
            }
            cnt++;
        }

        if ( N == "6174" ) {
            cout << cnt << endl;
        } else {
            cout << "NA" << endl;
        }
    }

    return ( 0 );
}

C++: #include<stdio.h>

int main(){

char ty;
char c1,c2,c3,c4,ch5;
int n1=0,n2=0,n3=0,n4=0,N=0,S=0,P=0,K=0;
int n[5]={};
int F=0;
while(1){
//n1=c1-'0';n2=c2-'0';n3=c3-'0';n4=c4-'0';
	
	bool f=false;
	do{
	scanf("%c%c%c%c%c",&c1,&c2,&c3,&c4,&ch5);
	ty=c1;
	
     if(ty==c1&&ty==c3&&ty==c4&&ty==c2)f=false;
	else f=true;

	if(ty=='0')f=true;
	if(f==false)printf("NA\n");

	}while(f!=true);
	if(c1=='0'&&c2=='0'&&c3=='0'&&c4=='0')break;
	F++;
	if(F==10001)break;
	
	for(int i=0;i<4;i++)
	n[i]=0;
int cnt=0;


n[0]=c1-'0';n[1]=c2-'0';n[2]=c3-'0';n[3]=c4-'0';


for(int i=0;i<3;i++)
for(int j=3;j>i;j--)
if(n[j]>n[j-1]){int t=n[j];n[j]=n[j-1];n[j-1]=t;}

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

P=N-S;
if(P!=6174)cnt++;

while(P!=6174){
N=P;
K=P;n[0]=N/1000;
K=N%1000;n[1]=K/100;
K=N%100;n[2]=K/10;
K=N%10;n[3]=K;

//printf(" %d\n",N);

for(int i=0;i<3;i++)
for(int j=3;j>i;j--)
if(n[j]>n[j-1]){int t=n[j];n[j]=n[j-1];n[j-1]=t;}


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

cnt++;
P=N-S;

}
printf("%d\n",cnt);
}
return 0;
}

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

int main()
{
	while (true){
		char num[4];
		int ans;
		for (int i = 0; i < 4; i++){
			cin >> num[i];
		}
		if (num[0] == num[1] && num[1] == num[2] && num[2] == num[3]){
			if (num[0] == '0') break;
			cout << "NA" << endl;
			continue;
		}
		for (ans = 0; num[0] != '6' || num[1] != '1' || num[2] != '7' || num[3] != '4'; ans++){
			char num2[4], num3[4];
			for (int i = 0; i < 4; i++){
				num2[i] = num[i];
			}
			sort(num, num + 4);
			sort(num2, num2 + 4, greater<char>());
			for (int i = 3; i >= 0; i--){
				if (num2[i] - num[i] < 0){
					num2[i] += 10;
					num2[i - 1]--;
				}
				num3[i] = num2[i] - num[i] + '0';
			}
			for (int i = 0; i < 4; i++){
				num[i] = num3[i];
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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



vector<int> i_v(int n){
  vector<int> res;
  while(n!=0){
    res.push_back(n%10);
    n/=10;
  }
  return res;
}

int v_i(vector<int> v){
  int res=0;
  for(int i=0;i<v.size();i++){
    res*=10;
    res+=v[v.size()-1-i];
  }
  return res;
}

int main(){
  int n;
  vector<int> l,s;
  while(cin>>n){
    l = i_v(n);
    bool f = true;
    for(int i=1;i<l.size();i++){
      if(l[i-1]!=l[i]){
        f=false;
      }
    }
    if(f){
      cout<<"NA"<<endl;
      continue;
    }
    int ans=0;
    while(n!=6174){
      l.clear(),s.clear();
      l = i_v(n);
      sort(l.begin(),l.end());
      for(int i=0;i<l.size();i++){
        s.push_back(l[l.size()-1-i]);
      }
      n = v_i(l)-v_i(s);
      ans++;
    }
    cout<<ans<<endl;
  }
}