Go: package main

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

func main() {
	var v int
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Print("NA")
			continue
		}
		count := 0
		for v != 6174 {
			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))
			v = l - s
			count++
		}
		fmt.Println(count)
	}
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    while(true){
	string s;
	ll ans = 0;
	cin >> s;
	if(s == "0000") break;
	else if(s[0] == s[1] && s[0] == s[2] && s[0] == s[3]){
	    cout << "NA" << endl;
	    continue;
	}
	while(s != "6174"){
	    vector<int> a(4);
	    vector<int> b(4);
	    a[0] = (int)(s[0] - '0');
	    a[1] = (int)(s[1] - '0');
	    a[2] = (int)(s[2] - '0');
	    a[3] = (int)(s[3] - '0');
	    b[0] = (int)(s[0] - '0');
	    b[1] = (int)(s[1] - '0');
	    b[2] = (int)(s[2] - '0');
	    b[3] = (int)(s[3] - '0');
	    sort(ALL(b));
	    sort(RALL(a));
	    int an = 1000 * a[0] + 100 * a[1] + 10 * a[2] + a[3];
	    int bn = 1000 * b[0] + 100 * b[1] + 10 * b[2] + b[3];
	    s = to_string(an - bn);
	    ans++;
	}
	cout << ans << endl;
    }
    return 0;
}

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

int main(){
  int n;

  while(cin >> n, n){
    n++;
    int t[102];

    for(int i = 0; i < n; i++){
      cin >> t[i];
    }

    int val1 = -1, val2 = -1;
    int cnt1 = 0, cnt2 = 0;

    for(int i = 1; i < n; i++){
      int diff = t[i] - t[i - 1];

      if(val1 == -1){
        val1 = diff;
        cnt1++;
      }
      else if(val1 == diff){
        cnt1++;
      }
      else{
        val2 = diff;
        cnt2++;
      }
    }

    if(cnt2 > cnt1){
      swap(val1, val2);
    }

    for(int i = 1; i < n; i++){
      int diff = t[i] - t[i - 1];

      if(val1 != diff){
        if(i == 1 && t[2] - t[1] == val1){
          cout << t[0] << endl;
        }
        else{
          cout << t[i] << endl;
        }
        break;
      }
    }
  }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <limits>
#include <cmath>
#include <cassert>

using namespace std;
using ll = long long;

const int INF = 1<<30;
const int MOD = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 5;
#define debug(x) cout << #x << ": " << x << endl

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string N;
    while(cin >> N, N != "0000")
    {
        int cnt = 0;
        char ch = N.front();
        bool ok = false;
        for(int i = 0; i < N.size(); i++) if(ch != N[i]) ok = true;
        if(ok == false)
        {
            cout << "NA" << endl;
            continue;
        }
        while(N != "6174")
        {
            string L = N, S = N;
            sort(L.begin(), L.end(), greater<char>());
            sort(S.begin(), S.end());
            N = to_string(stoi(L) - stoi(S));
            while(N.size() != 4) N.insert(N.begin(), '0');
            cnt++;
        }
        cout << cnt << endl;
    }
    return 0;
}

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 <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <cstdlib>

using namespace std;

int main(){

	while(1){
		int n=0,l,s;
		int cnt = 0;
		string tmp;

		cin >> tmp;
		n = atoi(tmp.c_str());

		if(n == 0)break;
		if(n % 1111 == 0){
			printf("NA\n");
			continue;
		}

		while(n != 6174){
			sort(tmp.begin(),tmp.end());
			s = atoi(tmp.c_str());

			sort(tmp.begin(),tmp.end(),greater<char>());
			l = atoi(tmp.c_str());
			n = l - s;

			for(int i=0,p=1000;i<4;i++,p/=10){
				tmp[i] = '0' + n/p;
				n %= p;
			}
			n = atoi(tmp.c_str());
			cnt++;
		}
		printf("%d\n", cnt);
	}
	return 0;
}

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

int main()
{
  while (true) {
    int n;
    cin >> n;

    if (n == 0) break;
    if (n % 1111 == 0) {
      cout << "NA" << endl;
      continue;
    }

    int cnt = 0;
    while (n != 6174) {
      cnt++;
      int l, s;
      int a[4];
      for (int i = 0; i < n; i++) {
        a[i] = n % 10;
        n /= 10;
      }

      sort(a, a + 4);
      s = a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3];
      l = a[3] * 1000 + a[2] * 100 + a[1] * 10 + a[0];

      n = l - s;
    }
    cout << cnt << endl;
  }
}

C++: #include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

string toStr(int a)
{
    ostringstream oss;
    oss << setfill('0') << setw(4) << a;
    return oss.str();
}

int main()
{
    for(;;){
        int n;
        cin >> n;
        if(n == 0)
            return 0;

        int ret = 0;
        while(n != 0 && n != 6174){
            string s = toStr(n);
            string t = s;
            sort(s.rbegin(), s.rend());
            sort(t.begin(), t.end());

            n = atoi(s.c_str()) - atoi(t.c_str());
            ++ ret;
        }

        if(n == 0)
            cout << "NA" << endl;
        else
            cout << ret << endl;
    }
}

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

int compute(string num){
  if ( num[0] == num[1] && num[1] == num[2] && num[2] == num[3] ) return -1;

  int cnt = 0;
  string L, S;
  while(1){
    if ( num == "6174" ) break;
    L = S = num;
    sort(L.begin(), L.end());
    reverse(L.begin(), L.end());
    sort(S.begin(), S.end());
    int x = atoi(L.c_str()) - atoi(S.c_str());
    num[0] = '0' + x/1000; x %= 1000;
    num[1] = '0' + x/100; x %= 100;
    num[2] = '0' + x/10; x %= 10;
    num[3] = '0' + x;
    cnt++;
  }
  return cnt;
}

main(){
  string num;
  while(1){
    cin >> num;
    if ( num == "0000" ) break;
    int a = compute(num);
    if ( a< 0 ) cout << "NA" << endl;
    else cout << a << endl;
  }
}