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 <bits/stdc++.h>
using namespace std;
// define
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a+mod-b)%mod
#define MUL(a, b) a = (((a)%mod) * ((b)%mod)) % mod
#define rollcall cout << "I'm Sucu." << endl;
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define dmt(x, y, z) make_tuple(x, y, z)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define fi first
#define se second
// typedef
typedef complex<double> Point;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9+7;
const int INF = 1e18+7;
// int sum = accumulate(a, a+n, 0LL);
// int mx = max_element(a, a+n);
// int mn = min_element(a, a+n);
// int cnt = count(a, a+n, k);


string to_str(int num){
  string str;
  drep(i,3,0){
    int c = 1;
    rep(j,i)c *= 10;
    str.pb(num/c + '0');
    num = num%c;
  }
  return str;
}

signed main(){
  while(true){
    string str;cin >> str;
    if(str == "0000")break;
    if(str[0] == str[1] and str[1] == str[2] and str[2] == str[3]){
      cout << "NA" << endl;continue;
    }
    int cnt = 0;
    while(true){
      if(str == "6174")break;
      string s1, s2;
      sort(all(str));
      s1 = str;
      reverse(all(str));
      s2 = str;
      int res = atoll(s2.c_str()) - atoll(s1.c_str());
      str = to_str(res);
      cnt++;
      // cout << s1 << " " << s2 << " " << str << endl;
    }
    cout << cnt << endl;
  }
  return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int N,L,S,N1,N2,N3,N4,N5,N6,i,temp,j,cnt,flg,flg1;
	int a[4];
	cnt=1;
	flg=0;
	flg1=0;
	scanf("%d",&N);
	if(N==6174){
		flg1=1;
	}
	while(!(N==0000)){
		if(flg1==1){
			cnt=0;
			flg=1;
			break;
		}
		else{
			N1=N/1000;/*6!*/
			N2=N%1000;/*174*/
			N3=N2/100;/*1!*/
			N4=N2%100;/*74*/
			N5=N4/10;/*7*/
			N6=N4%10;/*4!*/
			if(N1==N3 && N1==N5 && N1==N6){
				printf("NA\n");
				break;
			}
			else{
				a[0]=N1;
				a[1]=N3;
				a[2]=N5;
				a[3]=N6;
				for(i=0;i<4;i++){
					for(j=i+1;j<4;j++){
						if(a[i]<a[j]){
							temp=a[j];
							a[j]=a[i];
							a[i]=temp;	
						}
					}
				}
				N1=a[0]*1000;
				N3=a[1]*100;
				N5=a[2]*10;
				N6=a[3];
				L=N1+N3+N5+N6;
				for(i=0;i<4;i++){
					for(j=i+1;j<4;j++){
						if(a[i]>a[j]){
							temp=a[i];
							a[i]=a[j];
							a[j]=temp;	
						}
						
					}
				}
				N1=a[0]*1000;
				N3=a[1]*100;
				N5=a[2]*10;
				N6=a[3];
				S=N1+N3+N5+N6;
				N=L-S;
				if(N==6174){
				
					break;
				}
				else{
				flg=1;
					cnt++;
				}
			}
		}
	}
	if(flg==1){
	printf("%d\n",cnt);
	}
	return 0;
}

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

int main(){
  int n;
  int num[5];
  int l[5],s[5];
  int ans;

  while(1){
    cin >> n;
    if(n==0){
      break;
    }
    for(int i=3;i>=0;i--){
      num[i]=n%10;
      n/=10;
    }
    ans=0;
    while(1){
      if(num[0]==6 && num[1]==1 && num[2]==7 && num[3]==4){
	cout << ans << '\n';
	break;
      }
      if(num[0]==num[1] && num[1]==num[2] && num[2]==num[3]){
	cout << "NA\n";
	break;
      } 
      sort(num,num+4,greater<int>());
      for(int i=0;i<4;i++){
	l[i]=num[i];
      }
      int j=3;
      for(int i=0;i<4;i++){
	s[i]=num[j];
	j--;
      }
      
      for(int i=3;i>=0;i--){
	if(l[i]<s[i]){
	  l[i]+=10;
	  l[i-1]--;
	  num[i]=l[i]-s[i];
	}else{
	  num[i]=l[i]-s[i];
	}
      }
      ans++;
    }
  }
  
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    char s[8];
    while (atoi(fgets(s,8,stdin))) {
        *strchr(s,'\n')=0;
        int res=0;
        while (strcmp(s,"6174")) {
            sort(s, s+4);
            int S=atoi(s);
            reverse(s, s+4);
            int L=atoi(s);
            if (S==L) { res=-1; break; }
            res++;
            sprintf(s,"%04d",L-S);
        }
        if (res<0) puts("NA");
        else printf("%d\n",res);
    }
    return 0;
}

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

int main(){
	int num[4],l,s,aa=0,n;
	while(cin>>n,n){
		if(n>999){
			num[0]=n/1000;
			num[1]=n/100%10;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n>99&&n<1000){
			num[0]=0;
			num[1]=n/100%10;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n>9&&n<100){
			num[0]=0;
			num[1]=0;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n<10){
			num[0]=0;
			num[1]=0;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		aa=0;
		if(num[0]==num[1]&&num[1]==num[2]&&num[2]==num[3]){
			cout<<"NA"<<endl;
			break;
		}
		while(num[0]!=6&&num[1]!=1&&num[2]!=7&&num[3]!=4){
			l=0,s=0;
			sort(num,num+4);
			for(int i=0;i<4;i++)cout<<num[i];
			cout<<endl;
			s=num[0]*1000+num[1]*100+num[2]*10+num[3];
			l=num[3]*1000+num[2]*100+num[1]*10+num[0];
			num[0]=(l-s)/1000;
			num[1]=(l-s)/100%10;
			num[2]=(l-s)/10%10;
			num[3]=(l-s)%10;
			aa++;
		}
		cout<<aa<<endl;
	}
}

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

int main() {
	string N;
	
	while (cin >> N) {
		if (N == "0000") break;
		int ok = 1, ans = 0;
		set< string > strs;
		
		while (ok) {
			if (N == "6174") break;
			strs.insert(N);
			
			string L = N, S = N;
			sort(L.begin(), L.end(), greater< char >());
			sort(S.begin(), S.end());
			
			stringstream ss;
			ss << L << " " << S;
			int l, s;
			ss >> l >> s;
			
			int n = l - s;
			stringstream sss;
			sss << n;
			N = sss.str();
			
			if (strs.count(N)) ok = 0;
			
			++ans;
		}
		
		if (ok) cout << ans << endl;
		else cout << "NA" << endl;
	}
}