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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define rep(i,x) for(int i=0;i<(x);++i)
#define rep1(i,x) for(int i=1;i<=(x);++i)
#define rrep(i,x) for(int i=(x)-1;i>=0;--i)
#define rrep1(i,x) for(int i=(x);i>=1;--i)
#define FOR(i,a,x) for(int i=(a);i<(x);++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

#define omajinai ios::sync_with_stdio(false);cin.tie(0)

template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>istream&operator>>(istream&is,vector<T>&vec){rep(i,vec.size())is>>vec[i];return is;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

string f(string a)
{
    while (a.size() < 4) a = '0' + a;

    return a;
}

int to_int(string s)
{
    int res = 0;

    int i = 0;
    while (s[i] == '0') i++;

    for (; i < s.size(); ++i) {
        res *= 10;
        res += s[i] - '0';
    }

    return res;
}

string to_str(int a)
{
    string res = "";

    do {
        res += (a % 10) + '0';
        a /= 10;
    } while (a);

    reverse(all(res));

    return f(res);
}

int solve(int now, string N)
{
    if (N == "6174") return now;

    string l = N, s = N;
    sort(rall(l));
    sort(all(s));

    return solve(now + 1, to_str(to_int(l) - to_int(s)));
}

bool check(string a)
{
    char c = a[0];
    bool f = true;

    rep(i, a.size()) {
        if (a[i] != c) return true;
    }

    return false;
}

signed main()
{
    string N;
    
    while (true) {
        cin >> N;

        if (check(N)) {
            cout << solve(0, N) << endl;
        } else {
            if (N[0] == '0') {
                break;
            }

            cout << "NA" << endl;
        }
    }
}

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

int main() {
	int n;
	int str[4];
	bool used[10000];
	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;
		}
		std::memset(used, false, sizeof(used));
		used[n] = true;
		int cnt = 0;
		while(n!=6174) {
			if (str[0] == str[1] == str[2] == str[3]) {
				std::cout << "NA" << std::endl;
				goto a;
			}
			++cnt;
			std::sort(str, str + 4, std::greater<int>());
			int l = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
			std::sort(str, str + 4);
			int	s = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
			n = l - s;
			if (used[n])
				goto a;
			used[n] = true;
			str[0] = n / 1000;
			str[1] = (n / 100) % 10;
			str[2] = (n / 10) % 10;
			str[3] = n % 10;
		}
		std::cout << cnt << std::endl;
	a:;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<sstream>
#include<cmath>
#include<algorithm>
#include<cassert>
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){
  string res="0000";
  int tmp[4]={0,0,0,0};
  for(int i=0;n!=0;i++){
    tmp[i]=n%10;
    n/=10;
  }
  for(int i=0;i<4;i++){
    res[i]=tmp[i]+'0';
  }
  sort(res.begin(),res.end());
  return res;
}

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;
      assert(ans<=7);
    }
    if(n==6174){
      cout<<ans<<endl;
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
 
int main()
{
    string N;
    while ( cin >> N, N != "0000" ) {
        int cnt = 0;
        set < string > st;
 
        while ( N != "6174" && st.find(N) == st.end() ) {
            string L = N, S = N;
            int val = 0;
            st.insert(N);
            sort(rbegin(L), rend(L));
            sort(begin(S), end(S));
            val = atoi(L.c_str()) - atoi(S.c_str());
            N = to_string(val);
            while ( N.size() < 4 ) {
                N = '0' + N;
            }
            cnt++;
        }
 
        if ( N == "6174" ) {
            cout << cnt << endl;
        } else {
            cout << "NA" << endl;
        }
    }
 
    return ( 0 );
}

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

			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]>keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			s=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];
			n=l-s;
			cnt++;
		}
	printf("%d\n",cnt);
	scanf("%d",&n);
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(void){
	string s,ss,sss;
	int i,j;
	int x,y;
	int z;
	int flg;
	while(1){
		cin>>s;
		if(s=="0000")	break;
		flg=0;
		for(i=0;i<3;i++){
			if(s[i]!=s[i+1]){
				flg=1;
				break;
			}
		}
		if(flg==1){
			j=1;
			if(s=="6174")	j=0;
			else{
				while(1){
					if(s=="6174")	break;
					ss=s;
					sort(ss.begin(),ss.end(),greater<char>());
					if(ss=="6174")	break;
					sss=s;
					sort(sss.begin(),sss.end());
					if(sss=="6174")	break;
					z=1000;
					x=y=0;
					for(i=0;i<4;i++){
						x+=(ss[i]-'0')*z;
						y+=(sss[i]-'0')*z;
						z/=10;
					}
					//cout<<x<<" "<<y<<endl;
					z=x-y;
					//cout<<z<<endl;
					for(i=3;i>=0;i--){
						s[i]=(z%10)+'0';
						z/=10;
					}
					//cout<<s<<endl;
					if(s=="6174")	break;
					j++;
				}
			}
		}
		if(flg==1)	cout<<j<<endl;
		else 		cout<<"NA"<<endl;
	}
	return 0;
}