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]
}

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))
	}
}

Python: while True:
    n = input()
 
    if n == "0000":
        break
    if n.count(n[0]) == 4:
        print("NA")
        continue
 
    cnt = 0
    while n != "6174":
        n, l = sorted(list(n)), sorted(list(n), reverse=True)
        n = str(int("".join(l)) - int("".join(n))).zfill(4)
        cnt += 1
 
    print(cnt)

Python: while 1:
	line = list(raw_input())
	if line[0] == line[1] and line[1] == line[2] and line[2] == line[3]:
		if line[0] == "0":
			break
		print("NA")
		continue
	
	c = 0
	while 1:
		if "".join(line) == "6174":
			print(c)
			break
		else:
			line.sort()
			l = int("".join(line[::-1]))
			s = int("".join(line))
			line = list(str(l - s))
			c += 1

Python: while True:
    n = input()
    if n == "0000":
        break

    if n.count(n[0]) == 4:
        print("NA")
        continue

    cnt = 0
    while n != "6174":
        L, S = list(n), list(n)
        L.sort(reverse=True)
        S.sort()
        n = str(int("".join(L)) - int("".join(S))).zfill(4)
        cnt += 1

    print(cnt)

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <sstream>

const std::string answer="6174";

int main()
{
    std::string in_str;
	std::cin>>in_str;
	int L,S;
	int i;
	while(in_str[0]!='0'||in_str[1]!='0'||in_str[2]!='0'||in_str[3]!='0'){
		if(in_str[0]!=in_str[1]||in_str[1]!=in_str[2]||in_str[2]!=in_str[3]){
			for(i=0;in_str!=answer;i++){
                {
                    std::stringstream sss;
    				std::sort(in_str.begin(),in_str.end());
    				sss<<in_str;
    				sss>>S;
                }
                {
                    std::stringstream sss;
        			std::sort(in_str.begin(),in_str.end(),std::greater<char>());
    				sss<<in_str;
    				sss>>L;
                }
                L-=S;
                {
                    std::stringstream sss;
    				sss<<L;
    				in_str=sss.str();
                }
			}
			std::cout<<i<<std::endl;
		}else{
			std::cout<<"NA"<<std::endl;
		}
		std::cin>>in_str;
	}
}

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

using namespace std;

using i64 = int64_t;
using P = pair<i64, i64>;

struct edge {
    i64 from, to, cost;
    edge(i64 from, i64 to) : from(from), to(to), cost(1) {}
    edge(i64 from, i64 to, i64 cost) : from(from), to(to), cost(cost) {}
};
using Graph = vector<vector<edge>>;

constexpr i64 INF = std::numeric_limits<i64>::max();
constexpr i64 SINF = std::numeric_limits<i64>::max() / 10;
const i64 dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };
//const i64 dx[5] = {-1,0,0,0,1}, dy[5] = {0,-1,0,1,0};
//const i64 dx[8] = {-1,0,1,1,1,0,-1,-1}, dy[8] = {1,1,1,0,-1,-1,-1,0};
//const i64 dx[9] = {-1,0,1,1,1,0,-1,-1,0}, dy[9] = {1,1,1,0,-1,-1,-1,0,0};

#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)

// http://beet-aizu.hatenablog.com/entry/2018/04/08/145516
template<typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }

template<typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template<typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T& t, const V& v) { t = v; }

template<typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T& t, const V& v) {
    for (auto& e : t) fill_v(e, v);
}

struct IoSetup {
    IoSetup() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
        cerr << fixed << setprecision(10);
    }
} iosetup;

i64 solve(i64 n){
    if(n == 6174) return 0;
    ostringstream sout;
    sout << setfill('0') << std::setw(4) << n;
    string l = sout.str();
    string r = l;
    sort(l.begin(), l.end());
    sort(r.rbegin(), r.rend());
    i64 x = stoi(r) - stoi(l);
    if(x == 6174) return 1;
    else return solve(x) + 1;
}

int main(){
    while(true){
        i64 n;
        cin >> n;
        if(n == 0) break;
        if(n % 1111 == 0) cout << "NA" << endl;
        else cout << solve(n) << endl;
    }
}

C++: #include <iostream>
#include <string>

using namespace std;

int d[4];
string in;
int data[10000];

void solve(){
  bool f = true;
  for(int i=1;i<4;i++) if(in[0] != in[i]) f = false;
  if(f) cout << "NA" << endl;
  else{
    int ans = 0;
    for(int i=0;i<4;i++) d[i] = in[i] - '0';
    while(1){
      if(d[0] == 6 && d[1] == 1 && d[2] == 7 && d[3] == 4) break;
      int l = 0, s = 0;
      sort(d, d+4);
      for(int i=0;i<4;i++){
        s *= 10;
        s += d[i];
        l *= 10;
        l += d[3-i];
      }
      ans++;
      int tmp  = l - s;
      if(data[tmp] != -1){
        ans += data[tmp];
        break;
      }
      for(int i=0;i<4;i++){
        d[3-i] = tmp % 10;
        tmp /= 10;
      }
    }
    int tmp = 0;
    for(int i=0;i<4;i++){
      tmp *= 10;
      tmp += d[3-i];
    }
    data[tmp] = ans;
    cout << ans << endl;
  }
}

main(){
  fill(data, data+10000, -1);
  while(cin >> in){
    if(in == "0000") break;
    solve();
  }
}

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


bool allsame(int n){
  int tmp=n%10;
  n/=10;
  while(n!=0){
    if(tmp!=n%10){
      return 0;
    }
    tmp=n%10;
    n/=10;
  }
  return 1;
}

string toSortString(int n){
  stringstream ss;
  ss<<n;
  string tmp;
  ss>>tmp;
  sort(tmp.begin(),tmp.end());
  return tmp;
}

int make_l(string s){
  int res=0;
  for(int i=0;i<s.size();i++){
    res+=(s[i]-'0')*pow(double(10),i);
  }
  return res;
}

int make_s(string s){
  int res=0;
  for(int i=0;i<s.size();i++){
    res+=(s[s.size()-1-i]-'0')*pow(double(10),i);
  }
  return res;
}

int main(){
  int n;
  int ans=0;
  while(cin>>n,n!=0){
    while(n!=6174){
      if(allsame(n)){
        cout<<"NA"<<endl;
        break;
      }
      ans++;
      int l,s;
      string tmp_n=toSortString(n);
      l=make_l(tmp_n);
      s=make_s(tmp_n);
      n=l-s;
    }
    if(n==6174){
      cout<<ans<<endl;
    }
  }
}

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

using int64 = long long;

int main() {
	string str;
	while (true) {
		cin >> str;
		if (str == "0000") break;

		int cnt = 0;
		while (true) {
			if (str == "6174") {
				cout << cnt << endl;
				break;
			}
			if (str[0] == str[1] && str[1] == str[2] && str[2] == str[3]) {
				cout << "NA" << endl;
				break;
			}
			cnt++;
			string L = str;
			sort(L.rbegin(), L.rend());
			sort(str.begin(), str.end());

			int nl = stoi(L);
			int ns = stoi(str);


			int res = nl - ns;
			str = to_string(res);
			if (str.size() < 4) {
				for (int i = 1; i + str.size() <= 4; ++i) str.insert(str.begin(), '0');
			}
		}
	}
}