Go: package main

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

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		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([]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 <cstring>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;

string toString(int n){
  ostringstream oss;
  oss << n;
  return oss.str();
}

int toInt(string s){
  istringstream iss(s);
  int ret;
  iss >> ret;
  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;
    bool f = false;
    while(s != "6174"){
    if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]){
      cout <<"NA"<<endl;
      f = true;
      break;
    }
      cnt++;
      string L, S;
      L = s;
      sort(L.begin(), L.end());
      S = L;
      reverse(L.begin(), L.end());
      int a, b;
      a = toInt(L);
      b = toInt(S);
      s = toString(a-b);
      while(s.size() != 4){
	s += "0";
      }
    }
    if(!f)cout << cnt << endl;
  }
  return 0;
}

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

int N,a[4],S.L,count;

int main(){
	while(1){
		count=0;
		cin>>N;
		if(N==0){break;}
		if(N%1111==0){
			cout<<"NA"<<endl;
			continue;
		}
		while(N!=6174){
			a[0]=N/1000;
			a[1]=(N%1000)/100;
			a[2]=(N%100)/10;
			a[3]=N%10;
			sort(a,a+4);
			L=a[3]*1000+a[2]*100+a[1]*10+a[0];
			S=a[0]*1000+a[1]*100+a[2]*10+a[3];
			N=L-S;
			count++;
		}
		cout<<count<<endl;
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;

int main(){
	string c;
	while(cin>>c,c!="0000"){
		if(c[0]==c[1]&&c[0]==c[2]&&c[0]==c[3]){ printf("NA\n"); continue;}
		int res=0;
		while(c!="6174"){cout<<c<<endl;
			string l,m;
			l=m=c;
			sort(l.begin(),l.end(),greater<char>()); sort(m.begin(),m.end());
			int a=(l[0]-48)*1000+(l[1]-48)*100+(l[0]-48)*10+(l[1]-48);
			int b=(m[0]-48)*1000+(m[1]-48)*100+(m[0]-48)*10+(m[1]-48);
			int x=a-b;
			for(int i=0,p=1000;i<4;i++){ c[i]=(char)(48+x/p); p%=10;}
			res++;
		}
		printf("%d\n",res);
	}
}

C++: #include<stdio.h>
int main(void)
{
	int n,p,q,i,l,a[4],b,m,k,flg;
	scanf("%d",&n);
	while(1){
		if(n==0) break;
		p=0;q=0;m=0;flg=0;
		while(1){
			l=n;
			if(l==6174) break;
			flg=1;
			k=1000;
			for(i=0;i<4;i++){
				a[i]=n/k;
				n-=a[i]*k;
				k/=10;
			}
			for(i=0;i<3;i++){
				for(b=i+1;b<4;b++){
					if(a[i]<a[b]){
						k=a[i];
						a[i]=a[b];
						a[b]=k;
					}
				}
			}
			for(i=0;i<3;i++) if(a[i]!=a[i+1]) flg=0;
			if(flg==1) break;
			p=a[0]*1000+a[1]*100+a[2]*10+a[3];
			q=a[3]*1000+a[2]*100+a[1]*10+a[0];
			l=p-q;
			n=l;
			m++;
		}
		if(flg==1) printf("NA\n");
		else printf("%d\n",m);
		scanf("%d",&n);
	}
}

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") {
		if (NA(s))cout << "NA" << endl;
		else {
			int ans(0);
			while (s != "6174") {
				string big(s), small(s);
				sort(all(small));
				sort(all(big), greater<char>());
				s = to_string(stoi(big) - stoi(small));
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <sstream>

#define rep(i, j) FOR(i, 0, j)
#define FOR(i, j, k) for(int i = j; i < k; ++i)

using namespace std;

int main(){
  char n[5];
  while(scanf("%s", n) && strcmp(n, "0000") != 0){
    if(n[0] == n[1] && n[1] == n[2] && n[2] == n[3]){ puts("NA"); continue;}
    int c = 0;
    while(strcmp(n, "6174") != 0){
      rep(i, 4) FOR(j, i + 1, 4){
	if(n[i] > n[j]){ char tmp = n[i]; n[i] = n[j]; n[j] = tmp;}
      }
      int a = atoi(n);
      char tmp = n[0]; n[0] = n[3]; n[3] = tmp;
      tmp = n[1]; n[1] = n[2]; n[2] = tmp;
      int b = atoi(n);
      b -=  a;
      sprintf(n, "%d", b);
      c++;
    }
    printf("%d\n", c);
  }
  return 0;
}

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

string toString(int n){
  ostringstream oss;
  oss << n;
  return oss.str();
}

int toInt(string s){
  istringstream iss(s);
  int ret;
  iss >> ret;
  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;
    
    int cnt = 0;
    
    while(s != "6174"){
      cnt++;
      string L, S;
      L = s;
      sort(L.begin(), L.end());
      S = L;
      reverse(L.begin(), L.end());
      int a, b;
      a = toInt(L);
      b = toInt(S);
      s = toString(a-b);
    }
    cout << cnt << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

char s[10];
int main() {
	while (scanf("%s", s), strcmp(s, "0000")) {
		int cnt = 0;
		if (s[0] == s[1] && s[0] == s[2] && s[0] == s[3]) {
			puts("NA");
			continue;
		}
		while (strcmp(s, "6174")) {
			sort(s, s + 4); int a = atoi(s);
			reverse(s, s + 4); int b = atoi(s);
			sprintf(s, "%04d", b - a);
			cnt++;
		}
		printf("%d\n", cnt);
	}
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

vector<int> solve(vector<int> a,vector<int> b){
    string x,y;
    rep(i,a.size()){
        x+='0'+a[i];
    }
    rep(i,b.size()){
        y+='0'+b[i];
    }
    int r=stoi(x)-stoi(y);
    vector<int> ret;
    stringstream ss;
    ss<<r;
    string z=ss.str();
    string t="";
    rep(i,4-z.size())t+="0";
    z=t+z;
    rep(i,z.size())ret.pb(z[i]-'0');
    return ret;
}

int main(){
    string s;
    while(cin>>s){
        if(s=="0000")break;
        int x=0;
        vector<int> c;
        rep(i,s.size())c.pb(s[i]-'0');
        while(1){
            if(c[0]==6&&c[1]==1&&c[2]==7&&c[3]==4){cout<<x<<endl;break;}
            if(c[0]==c[1]&&c[1]==c[2]&&c[2]==c[3]){
                cout<<"NA"<<endl;
                break;
            }
            vector<int> l=c,s=c;
            sort(all(s));
            sort(all(l),greater<int>());
            c=solve(l,s);
            x++;
        }
    }
}