Go: package main

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

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 {
		var s string
		fmt.Scan(&s)
		v, _ = strconv.Atoi(s)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

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

int main(void) {
	int S, L;
	string N;
	int ans;
	bool flag = false;

	while(1) {
flag=false;
		cin >> N;
		if(N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
			flag = true;
			if(N[0] == '0')
				break;
		}

		if(!flag) {
			for(ans = 0; atoi(N.c_str()) != 6174; ans++) {
				sort(N.begin(), N.end());
				S = atoi(N.c_str());
				sort(N.begin(), N.end(), greater<int>());
				L = atoi(N.c_str());
				int tmp = L - S;

				for(int i = 1000, j = 0; i; i /= 10, j++) {
					N[j] = tmp / i + '0';
					tmp %= i;
				}
			}
		}

		if(flag)
			cout << "NA" << endl;
		else
			cout << ans << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string Int2Str(int);
int Str2Int(string);
int main(){
  int l,s,cal;
  string str,L,S;
  while(1){
    cin >> str;
    if(str=="0000") break;
    int cnt=0,flag=0;
    if(str=="6174") cnt--;
    while(1){
      cnt++;
      sort(str.begin(),str.end());
      S=str;
      reverse(str.begin(),str.end());
      L=str;
      l=Str2Int(L);
      s=Str2Int(S);
      cal=l-s;
      if(cal==6174){
	flag=1;
	break;
      }
      if(cal==0||cal==1111||cal==2222||cal==3333||cal==4444||cal==5555||cal==6666||cal==7777||cal==8888||cal==9999){
	break;
      }
      str=Int2Str(cal);
    }
    if(flag==1) cout << cnt << endl;
    else cout << "NA" << endl;
  }
  return 0;
}
string Int2Str(int n){
  int p;
  string ret;
  while(n){
    p=n%10;
    ret=(char)(p+'0')+ret;
    n/=10;
  }
  if(ret.size()!=4){
    while(ret.size()!=4){
      ret+='0';
    }
  }
  return ret;
}
int Str2Int(string str){
  int ret=0,i=0,x=1,len=4;
  while(len){
    ret=ret*x+(str[i]-'0');
    i++;
    x=10;
    len--;
  }
  return ret;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define reps(i, m, n) for (int i = m; i <= n; ++i)
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;

signed main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  while (cin >> N, N) {
    int cnt = 0;
    bool isNA = false;
    while (N != 6174) {
      int tmp[20];
      rep(i, 4) {
        tmp[i] = N % 10;
        N /= 10;
      }
      if( tmp[0] == tmp[1] && tmp[1] == tmp[2] && tmp[2] == tmp[3] ) {
        isNA = true;
        break;
      }
      else {
        sort(tmp, tmp + 4, greater<int>());
        int L = tmp[0] * 1000 + tmp[1] * 100 + tmp[2] * 10 + tmp[3];
        int S = tmp[3] * 1000 + tmp[2] * 100 + tmp[1] * 10 + tmp[0];
        N = L - S;
        ++cnt;
        if( N == 6174 ) break;
      }
    }
    if( isNA ) cout << "NA" << endl;
    else cout << cnt << endl;
  }
  return 0;
}

C++: #include <cassert>

#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
//#include <boost/foreach.hpp>
//#include <boost/range/algorithm.hpp>
#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define ll long long
#define Sort(v) sort(all(v))
#define INF 1e9
#define LINF 1e18
#define END return 0
#define pb push_back
#define se second
#define fi first
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define MP make_pair
#define MOD 1000000007LL
#define int long long
using namespace std;
int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
struct edge{int to,cost;};
//typedef pair<int,int> P;

bool isupper(char c){if('A'<=c&&c<='Z')return 1;return 0;}
bool islower(char c){if('a'<=c&&c<='z')return 1;return 0;}
bool isPrime(int x){if(x==1)return 0;if(x==2)return 1;if(x%2==0)return 0;for(int i=3;i*i<=x;i++)if(x%i==0)return 0;return 1;}
bool iskaibun(string s){for(int i=0;i<s.size()/2;i++)if(s[i]!=s[s.size()-i-1])return 0;return 1;}
bool isnumber(char c){return ('0'<=c&&c<='9');}
bool isalpha(char c){return (isupper(c)&&islower(c));}
void printvi(vector<int> v){rep(i,0,v.size()){if(i)cout<<" ";cout<<v[i];}cout<<endl;}
void printvil(vector<int> v){rep(i,0,v.size()){cout<<v[i]<<endl;}}
void printvvi(vector<vector<int>> v){
    rep(i,0,v.size()){
        rep(j,0,v[i].size()){
            if(j)cout<<" ";
            cout<<v[i][j];
        }
        cout<<endl;
    }
}
void printvstr(vector<string> v){
    rep(i,0,v.size()){
        cout<<v[i]<<endl;
    }
}

signed main (){
    string s;
    while(1){
        cin>>s;
        if(s=="0000")break;
        auto cmp=[&](char a,char b){
            return a>b;
        };
        if(s[0]==s[1] and s[1]==s[2] and s[2]==s[3]){
            cout<<"NA"<<endl;
            continue;
        }
        int i=0;
        for(;s!="6174";i++){
            sort(all(s));
            int a1=stol(s);
            sort(all(s),cmp);
            int a2=stol(s);
            int a3=a2-a1;
            s=to_string(a3);
            while(s.size()<4){
                s="0"+s;
            }
            //cout<<s<<endl;
        }
        cout<<i<<endl;

    }

    
}
/*
*/

C++: while 1:
    N = input()
    if N=='0000':break
    if N[0] ==N[1] == N[2] == N[3]:
        print('NA')
        continue
    cnt = 0
    while N != '6174':
        x = int(''.join(reversed(sorted(list(N))))) - int(''.join(sorted(list(N))))
        N = str(x).zfill(4) 
        cnt += 1

    print(cnt)