Go: package main

import (
	"fmt"
	"sort"
)

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]
	return solve(l-s) + 1
}

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 <cstdio>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <stack>
#include <climits>
#include <deque>
#include <bitset>
#include <cassert>
#include <ctime>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
const int dy[]={-1,0,1,0},dx[]={0,1,0,-1};
// adjust problem by problem
const double EPS=1e-8;
const double PI=acos(-1.0);
#ifdef __GNUC__
int popcount(int n){return __builtin_popcount(n);}
int popcount(ll n){return __builtin_popcountll(n);}
#endif
#ifndef __GNUC__
template<class T> int popcount(T n){int cnt=0;while(n){if(n%2)cnt++;n/=2;}return cnt;}
#endif
template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> void PrintSeq(T &a,int sz){for(int i=0;i<sz;i++){cout<<a[i];if(sz==i+1)cout<<endl;else cout<<' ';}}
bool EQ(double a,double b){return abs(a-b)<EPS;}
void fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);}
vector<string> split(string str,char del){
  vector<string> res;
  for(int i=0,s=0;i<SIZE(str);i++){
    if(str[i]==del){if(i-s!=0)res.push_back(str.substr(s,i-s));s=i+1;}
    else if(i==SIZE(str)-1){res.push_back(str.substr(s));}
  }
  return res;
}

int main(){

  string s;
  while(cin>>s&&s!="0000"){
    set<char> st;
    for(int i=0;i<4;i++)
      st.insert(s[i]);
    if(st.size()==1)cout<<"NA"<<endl;
    else{
      for(int i=0;;i++){
        if(s=="6174"){
          cout<<i<<endl;
          break;
        }
        string ls,ss;
        sort(s.begin(),s.end());
        ss=s;
        sort(s.begin(),s.end(),greater<char>());
        ls=s;
        int diff=StringToInt<int>(ls)-StringToInt<int>(ss);
        string ns=IntToString(diff);
        while(ns.size()<4)ns='0'+ns;
        s=ns;
      }
    }
  }

  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
	string a;
	while (cin >> a&&a != "0000") {
		int b[4], l = 0, s = 0, f = atoi(a.c_str()), sum = 0;
		if (a[0] != a[1] && a[1] != a[2] && a[2] != a[3]) {
			while (f != 6174) {
				a = to_string(f);
				for (int i = 0; i < 4; i++) {
					b[i] = (int)a[i] - '0';
				}
				sort(b, b + 4);
				for (int i = 3; i >= 0; i--) {
					l += (pow(10, i)*b[i]);
				}
				for (int i = 0; i < 4; i++){
					s += (pow(10,3 - i)*b[i]);
				}
				f = l - s;
				sum++;
				l = 0;
				s = 0;
			}
			cout << sum << endl;
		}
		else 
			cout << "NA" << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef pair<int,string> P;
#define INF (1<<20)

int main(){
  string n;
  while(cin >> n , n!="0000"){
    if(n[0] == n[1] && n[1] == n[2] && n[2] == n[3]){
      cout << "NA" << endl;
      continue;
    }
    int cnt = 0;
    while(n != "6174"){
      cnt++;
      for(int i=0;i<4;i++){
	for(int j=i+1;j<4;j++){
	  if(n[i] < n[j]) swap(n[i],n[j]);
	}
      }
      string next = n;
      swap(next[0],next[3]);
      swap(next[1],next[2]);
      int L=0,S=0;
      for(int i=0;i<4;i++){
	L *= 10;
	S *= 10;
	L += n[i] - '0';
	S += next[i] - '0';
      }
      int N = L - S;
      n = "";
      for(int i=0;i<4;i++){
	int a = N % 10;
	n += ('0'+a);
	N /= 10;
      }
      swap(n[0],n[3]);
      swap(n[1],n[2]);
    }
    if(n == "6174") cout << cnt << endl;
  }
}

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]={};

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;

	}while(f!=true);
	if(c1=='0'&&c2=='0'&&c3=='0'&&c4=='0')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 <fstream>
#include <sstream>
#include <iomanip> 
#include <complex> 
#include <string>
#include <vector> 
#include <list>
#include <deque> 
#include <stack> 
#include <queue> 
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm> 
#include <numeric> 
#include <typeinfo> 
#include <cstdio>
#include <cstdlib> 
#include <cstring>
#include <cmath>
#include <climits> 
#include <ctime>
using namespace std;

string str;
int ans=0;

int main(void){
	while(1){
		cin >> str;
		ans=0;
		bool ch=true;
		if(str=="0000")break;
		if(str[0]==str[1] && str[1]==str[2] && str[2]==str[3])cout << "NA" << endl;
		else{
			while(1){
				if(str=="6174")break;
				int na,ni,nn;
				string min=str;
				sort(min.begin(),min.end());
				string max=min;
				reverse(max.begin(),max.end());
				stringstream ss;
				ss << min;
				ss >> ni;
				stringstream sk;
				sk << max;
				sk >> na;
				nn=na-ni;
				stringstream sc;
				sc << nn;
				string nstr;
				string sstr;
				sc >> nstr;
				while(nstr.size()+sstr.size()<4)sstr+="0";
				sstr+=nstr;
				str=sstr;
				ans++;
			}
			cout << ans << endl;
		}
	}
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main()
{
    string N;
    while (cin >> N, N != "0000") {
        int cnt = 0;
        for (int i = 0; i < 114514; i++, cnt++) {
            if (N == "6174") break;                
            string L = N, S = N;
            sort(L.begin(), L.end(), greater<char>());
            sort(S.begin(), S.end());
            auto diff = stoi(L) - stoi(S);
            N = to_string(diff);
        }
        if (cnt == 114514) {
            cout << "NA" << endl;
        } else {
            cout << cnt << endl;
        }
    }        
    return 0;
}