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 <string>
#include <algorithm>
using namespace std;

int main() {


  string n;

  while(1) {
    cin >>n;
    
    int flg = 0;
    for(int i=0;i<4;i++) if(n[i] != '0') flg =1;
    if(flg == 0) break;
    
    int a[4];
    for(int i=0;i<4;i++) a[i] = n[i]-'0';
    int N=0;
    int t =1;
    for(int i=0;i<4;i++) N += a[3-i]*t,t*=10;
    int ans = 0;
    while(1) {
      if( N == 6174 || ans > 10000) break;
      ans++;
      sort(a,a+4);
      int maxn = 0;
      int minn = 0;
      t = 1;
      for(int i=0;i<4;i++) {
	maxn += a[i] * t;
	minn += a[3-i] * t;
	t*=10;
      }
      
      N = maxn - minn;
      t = N;
      for(int i=0;i<4;i++) a[i] = t%10, t/=10;
    }
    if(ans > 10000) cout <<"NA"<<endl; 
    else     cout << ans << endl;
  }

    
    return 0;    
    
    
}

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

int main () {
    string str;
    while (cin >> str) {
        bool f = false;
        for (auto i : str) if (i != '0') f = true;

        if (!f) break;

        f = false;
        for (auto i : str) if (i != str[0]) f = true;

        if (!f) {
            cout << "NA" << endl;
            continue;
        }

        int count = 0;
        int now;
        stringstream org;
        org << str;
        org >> now;
        while (1) {
            if (now == 6174) {
                cout << count << endl;
                break;
            }
            count++;
            sort(str.begin(), str.end());
            auto rstr = str;
            reverse(rstr.begin(), rstr.end());

            stringstream ss;
            ss << rstr << " " << str;
            int A, B;
            ss >> A >> B;
            now = A - B;
            stringstream next;
            next << now;
            next >> str;
        }
    }

    return 0;
}

C++: //============================================================================
// Name        : AOJ.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdio>
#include <string>
#include <cctype>
using namespace std;

int main() {
	string n,L,S;
	int R=0;
	while(1){
		cin>>n;
		if(n=="0000")break;
		if(n=="6174")cout<<"0"<<endl;
		L=S=n;
		sort(L.begin(),L.end(),greater<char>());
		sort(S.begin(),S.end());

		cout<<R<<endl;
	}



	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};
    //cout<<nn[3]<<nn[2]<<nn[1]<<nn[0]<<endl;
    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;
	    }
	  }
	}
	//cout<<nn[3]<<nn[2]<<nn[1]<<nn[0]<<' '<<nn[0]<<nn[1]<<nn[2]<<nn[3]<<endl;
	for(int i=0;i<4;i++){
	  rn[i]=nn[i];
	}
	for(int i=0;i<4;i++){
	  if(rn[i]-nn[3-i]<0){
	    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];
	}
	//cout<<nn[3]<<nn[2]<<nn[1]<<nn[0]<<endl;
	cou++;
      }
      cout<<cou<<endl;
    }
    cin>>n;
  }
  return (0);
}

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

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    while (cin >> s) {
        if (s == "0000") return 0;
        vector<int> nums(4);
        for (int i = 0; i < 4; ++i) {
            nums[i] = s[i] - '0';
        }
        bool ok = false;
        for (int i = 0; i < 3; ++i) {
            if (nums[i] != nums[i + 1]) {
                ok = true;
                break;
            }
        }
        if (!ok) {
            cout << "NA" << '\n';
        } else {
            int res = 0;
            int nnow = 1000;
            for (int i = 0; i < 4; ++i) {
                res += nums[i] * nnow;
                nnow /= 10;
            }
            int cnt = 0;
            while (true) {
                if (res == 6174) break;
                vector<int> num(4);
                for (int i = 0; i < 4; ++i) {
                    num[i] = res % 10;
                    res /= 10;
                }
                sort(num.begin(), num.end());
                int large = 0, small = 0;
                int now = 1;
                for (int i = 0; i < 4; ++i) {
                    large += num[i] * now;
                    now *= 10;
                }
                now = 1;
                for (int i = 3; i >= 0; --i) {
                    small += num[i] * now;
                    now *= 10;
                }
                res = large - small;
                ++cnt;
            }
            cout << cnt << '\n';
        }
    }
    return 0;
}

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

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for(int i = 0; i < (n); i++)
#define dep(i, a, n) for(int i = a; i >= (n); i--)
#define FOR(i, a, n) for(int i = a; i < (n); i++)
#define mod (ll)(1e9+7)
#define int ll
#define INF 100000000

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int ch(int n) {
  vector<int> v;
  rep(i, 4) {
    v.pb(n % 10);
    n /= 10;
  }
  sort(v.begin(), v.end());
  int la = 0, sm = 0;
  rep(i, 4) {
    la += v[i] * pow(10, i);
    sm += v[3 - i] * pow(10, i);
  }
  return la - sm;
}

signed main() {
  ll n;
  while(cin >> n) {
    if(n == 0000) break;
    if(n % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }
    ll cnt = 0;
    while(1) {
      if(n == 6174) {
        cout << cnt << endl;
        break;
      }
      n = ch(n);
      cnt++;
    }
  }
	return 0;
}

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

int main(){
    int n, d[4];
    while(cin >> n){
        if(n == 0)  break;
        
        if(n % 1111 == 0){
            cout << "NA" << endl;
        }else{
            int tmpn = n;
            for(int i = 3; i >= 0; i--){
                d[i] = tmpn%10;
                tmpn /= 10;
            }
            
            int ans = 0;
            while(n != 6174){
                ans++;
                sort(d, d+4);
                n = (1000*d[3] + 100*d[2] + 10*d[1] + d[0]) -
                        (1000*d[0] + 100*d[1] + 10*d[2] + d[3]);
                tmpn = n;
                for(int i = 3; i >= 0; i--){
                    d[i] = tmpn%10;
                    tmpn /= 10;
                }
            }
            
            cout << ans << endl;
        }
    }
    return 0;
}