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<algorithm>
using namespace std;
int solve(int num,int& cnt) {
	if (num == 6174)return cnt;
	cnt++;
	int s[5];
	for (int i = 0; i < 4; i++) {
		s[i] = num % 10;
		num -= s[i];
		num /= 10;
	}
	sort(s, s + 4);
	return solve((s[3] - s[0]) * 1000 + (s[2] - s[1]) * 100 + (s[1] - s[2]) * 10 + (s[0] - s[3]), cnt);
}
int main() {
	int n;
	while (cin >> n) {
		if (!n)return 0;
		else if (n % 1111 == 0) {
			cout << "NA\n";
			continue;
		}
		int cnt;
		solve(n, cnt);
		cout << cnt << endl;
	}
}

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

int solve(int);

int main(){
	int N,ans=0;
	while(true){
		cin >> N;
		if(N == 0)
			break;
		if(N/1000 == (N%1000)/100 && (N%1000)/100 == (N%100)/10 && (N%100)/10 == N%10)
			cout << "NA" << endl;
		else{
			while(N != 6174){
				N = solve(N);
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

int solve(int N){
	int a[4],L,S;
	a[0] = N/1000;
	a[1] = (N%1000)/100;
	a[2] = (N%100)/10;
	a[3] = N%10;
	sort(a, a+4);
	S = a[3] + a[2]*10 + a[1]*100 + a[0]*1000;
	L = a[0] + a[1]*10 + a[2]*100 + a[3]*1000;
	return L-S;
}

C++: #include<stdio.h>
int main(void)
{
	int n,h[4],s[4],k[4],i,j,temp,flg,cnt;
	while(1){
		cnt=0;
		scanf("%d",&n);
		if(n==0000)break;
		if(n<1000){
			h[0]=0;
		}
		else{
			h[0]=n/1000;
		}
		if(n<100){
			h[1]=0;
		}
		else{
			h[1]=(n%1000)/100;
		}
		if(n<10){
			h[2]=0;
		}
		else{
			h[2]=((n%1000)%100)/10;
		}
		h[3]=(((n%1000)%100)%10)/1;
		while(1){
			if(h[0]==h[1]&&h[0]==h[2]&&h[0]==h[3]){
			printf("NA\n");
			break;
		}
			if(n==6174)break;
			cnt++;
			for(i=0;i<4;i++){
				s[i]=h[i];
				k[i]=h[i];
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(s[i]>s[j]){
						temp=s[i];
						s[i]=s[j];
						s[j]=temp;
					}
				}
				for(j=i+1;j<4;j++){
					if(k[i]>k[j]){
						temp=k[i];
						k[i]=k[j];
						k[j]=temp;
					}
				}
			}
			n=(s[0]*1000+s[1]*100+s[2]*10+s[3]*1)+(k[0]*1000+k[1]*100+k[2]*10+k[3]*1);
			if(n==6174)break;
			if(n<1000){
			h[0]=0;
			}
			else{
				h[0]=n/1000;
			}
			if(n<100){
				h[1]=0;
			}
			else{
				h[1]=(n%1000)/100;
			}
			if(n<10){
				h[2]=0;
			}
			else{
				h[2]=((n%1000)%100)/10;
			}
			h[3]=(((n%1000)%100)%10)/1;
		}
		if(cnt>0){
			printf("%d\n",cnt);
		}
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
  char c[4];
  while(cin>>c[0]>>c[1]>>c[2]>>c[3]){
    if(c[0]=='0'&&c[1]=='0'&&c[2]=='0'&&c[3]=='0') break;
    int su[4];
    int mems[4];
    int count = 0;
    int d = 1000;
    int h=1;
    for(int i=0;i<4;i++) su[i] = c[i] - '0';
    while(su[0]!=6||su[1]!=1||su[2]!=7||su[3]!=4){
      if(su[0]==0&&su[1]==0&&su[2]==0&&su[3]==0){
	cout << "NA" << endl;
	h = 0;
	break;
      }
      sort(su,su+4);
      for(int i=0;i<4;i++) mems[i] = su[i];
      sort(su,su+4,greater<int>());
      for(int i=0;i<4;i++) su[i] -= mems[i];
	for(int i=4;i>=1;i--){
	  if(su[i]<0){
	    su[i-1] -= 1;
	    su[i] += 10;
	  }
	}
	count++;
      }
    if(h==1) cout << count << endl;
  }
  return 0;
}

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

using namespace std;

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

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>

using namespace std;

int main(){
	int work[4],L,S,Result,count;
	char buf[5];

	while(true){
		scanf("%s",buf);

		if(buf[0] == '0' && buf[1] == '0' && buf[2] == '0' && buf[3] == '0')break;

		if(buf[0] == buf[1] && buf[0] == buf[2] && buf[0] == buf[3]){
			printf("NA\n");
			continue;
		}

		work[0] = buf[0] - '0';
		work[1] = buf[1] - '0';
		work[2] = buf[2] - '0';
		work[3] = buf[3] - '0';

		count = 0;

		while(true){
			if(work[0] == 6 && work[1] == 1 && work[2] == 7 && work[3] == 4)break;

			count++;
			for(int i = 1; i <= 3; i++){
				for(int k = 3; k >= i; k--){
					if(work[k] > work[k-1])swap(work[k],work[k-1]);
				}
			}

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

			for(int i = 1; i <= 3; i++){
				for(int k = 3; k >= i; k--){
					if(work[k] < work[k-1])swap(work[k],work[k-1]);
				}
			}
			S = 1000*work[0]+100*work[1]+10*work[2]+work[3];
			Result = L - S;

			for(int i = 0,k=1000; i < 4; i++, k /= 10){
				work[i] = Result/k;
				Result %= k;
			}
		}
		printf("%d\n",count);
	}

	return 0;
}

C++: #include<bits/stdc++.h>
#define endl '\n'
#define INF (1 << 21)
const int MOD = 1000000007;
using namespace std;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  string str;
  int n;
  int l, s;
  int ans;

  cin >> str;
  while(str != "0000"){
    if(str == "6174"){
      cout << 0 << endl;
    }else if(str[0] == str[1] && str[1] == str[2] && str[2] == str[3]){
      cout << "NA" << endl;
    }else{
      ans = 0;

      while(1){
	l = s = 0;
	ans ++;

	int r = 1;
	sort(str.begin(), str.end());
	for(int i = 3;i >= 0;i--){
	  s += (str[i] - '0') * r;
	  r *= 10;
	}
	
	r = 1;
	reverse(str.begin(), str.end());
	for(int i = 3;i >= 0;i--){
	  l += (str[i] - '0') * r;
	  r *= 10;
	}
	
	if((n = l - s) == 6174){
	  break;
	}else{
	  for(int i = 3;i >= 0;i--){
	    str[i] = (n % 10) + '0';
	    n /= 10;
	  }
	}
      }
      cout << ans << endl;
    }      
    cin >> str;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int m,n,l;
  cin>>n;
  while(n!=0){
    int nn[4]={n%10,(n/10)%10,(n/100)%10,(n/1000)%10};
    if(nn[0]==nn[1]&&nn[1]==nn[2]&&nn[2]==nn[3]){
      cout<<"NA"<<endl;
    }else{
      int cou=0;
      while(nn[0]!=4&&nn[1]!=7&&nn[2]!=1&&nn[3]!=6){
	int rn[4];
	for(int i=0;i<3;i++){
	  for(int j=i+1;j<4;j++){
	    if(nn[i]>nn[j]){
	      int exi=nn[i];
	      nn[i]=nn[j];
	      nn[j]=exi;
	    }
	  }
	}
	for(int i=0;i<4;i++){
	  rn[i]=nn[i];
	}
	for(int i=0;i<4;i++){
	  if(nn[3-i]>nn[i]){
	    rn[i]+=10;
	    rn[i]-=nn[3-i];
	    rn[i+1]--;
	  }else{
	    rn[i]-=nn[3-i];
	  }
	}
	for(int i=0;i<4;i++){
	  nn[i]=rn[i];
	}
	cou++;
      }
      cout<<cou<<endl;
    }
    cin>>n;
  }
  return (0);
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<cstdlib>

int main() {
	int n;
	int str[4];
	while (std::cin >> n) {
		int str[4] = { (n / 1000),(n / 100) % 10,(n / 10) % 10,n % 10 };
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == '0')
				break;
			else
				std::cout << "NA" << std::endl;
		}
		int l[4], s[4];
		int cnt = 0;
		while(n!=6174) {
			if (str[0] == str[1] == str[2] == str[3])
				std::cout << "NA" << std::endl;
			++cnt;
			std::memcpy(l, str, 4);
			std::memcpy(s, str, 4);
			std::sort(l, l + 5, std::greater<char>());
			std::sort(s, s + 5);
			int _l = l[0] * 1000 + l[1] * 100 + l[2] * 10 + l[3],
				_s = s[0] * 1000 + s[1] * 100 + s[2] * 10 + s[3];
			n = l - s;
			str[0] = (n / 1000) % 10;
			str[1] = (n / 100) % 10;
			str[2] = (n / 10) % 10;
			str[3] = n % 10;
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}