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<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){
  stringstream ss;
  ss<<n;
  string tmp;
  ss>>tmp;
  sort(tmp.begin(),tmp.end());
  for(;tmp.size()!=4;){
    tmp+='0'+tmp;
  }
  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;
      assert(ans<=7);
    }
    if(n==6174){
      cout<<ans<<endl;
    }
  }
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int main(){

    string c;
    while(1){
        cin>>c;
        if(c[0]==c[1]&&c[1]==c[2]&&c[2]==c[3]){
            if(c[0]=='0')break;
            cout<<"NA"<<endl;
            continue;
        }
        int a[4];
        for(int i=0;i<4;i++){
            a[i] = c[i] - '0';
        }
        int t=0;
        while(t++,1){
            if(a[0]==6&&a[1]==1&&a[2]==7&&a[3]==4){
                cout<<t-1<<endl;
                break;
            }
            int L,S;
            sort(a,a+4);
            L = a[0]+10*a[1]+100*a[2]+1000*a[3];
            reverse(a,a+4);
            S = a[0]+10*a[1]+100*a[2]+1000*a[3];
            int next = L - S;
            a[3] = next%10;
            a[2] = (next/10)%10;
            a[1] = (next/100)%10;
            a[0] = (next/1000)%10;
        }

    }
    return 0;
}

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

using namespace std;

int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

int main(void)
{
	string n;
	while (cin >> n && n != "0000"){
		if (count(n.begin(), n.end(), n[0]) == 4){
			cout << "NA" << endl;
		}
		assert(n != "0000");

		int tmp = toInt(n);
		char str[5];
		sprintf(str, "%04d", tmp);
		n = string(str);
		int cnt = 0;
		while (n != "6174"){
			string L = n; sort(L.rbegin(), L.rend());
			string S = n; sort(S.begin(), S.end());
			// cout << n << " " << L << " " << S << endl;
			int tmp = toInt(L) - toInt(S);
			char str[5];
			sprintf(str, "%04d", tmp);
			n = string(str);
			cnt++;
		}
		cout << cnt << endl;
	}

	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> pl;

int main()
{
	while (1) {
		string s;
		cin >> s;
		if (s[0] == '0' && s[1] == '0' && s[2] == '0' && s[3] == '0')
			break;
		if (s == "6174") {
			cout << 0 << endl;
		}
		else if (s[0] == s[1] &&  s[2] == s[3] && s[1] == s[2])
			cout << "NA" << endl;
		else {
			ll ans = 0;
			while (s != "6174") {
				while (s.size() != 4)
					s = '0' + s;
				
				int a[4];
				for (int i = 0; i < 4; i++)
					a[i] = s[i] - '0';

				sort(a, a + 4);

				ll small=0,large=0;
				for (int i = 0; i < 4; i++)
					large += a[i] * (ll)pow(10, i);
				
				reverse(a,a+4);
				for (int i = 0; i < 4; i++) 
					small += a[i] * (ll)pow(10, i);
				
				s = to_string(large - small);
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

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 = num;
    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;
  int tcase = 0;
  while(1){
    cin >> num;
    if ( num == "0000" ) break;
    tcase++;
    int a = compute(num);
    if ( a< 0 ) cout << "NA" << endl;
    else cout << a << endl;
  }
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#include<string>
#include<boost/lexical_cast.hpp>
using namespace std;

int main(){
  while(1){
    string N, L, S;
    cin >> N;
    if(N == "0000") break;
    
    int ans = 0;
    while(1){
      if(N == "6174"){
        cout << ans << endl;
        break;
      }
      
      L = N;
      S = N;
      
      sort(L.begin(), L.end(), greater<char>());
      sort(S.begin(), S.end());
      
      if(L == S){
        cout << "NA" << endl;
        break;
      }
      
      int n = boost::lexical_cast<int>(L) - boost::lexical_cast<int>(S);
      N = boost::lexical_cast<string>(n);
      
      ans++;
    }
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define F first
#define S second
#define endl '\n'
#define lcm( a, b ) ((a)/__gcd(a,b)*(b))
#define MP make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()

using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<int, P> i_P;
typedef pair< LL, LL > L_P;

const int INF = INT_MAX;
const int MIN = INT_MIN;
const LL LINF = LLONG_MAX;
const int MOD = 1000000000 + 7;
const int SIZE = 100005;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while( true ) {
        string nu;
        int n;
        cin >> nu;
        if( nu == "0000" ) break;
        int res = 0;

        bool flg = true;
        int cnt[10] = {};

        int dig = 1;
        n = 0;
        for( int i = 3; i >= 0; --i ) {
            n += ( nu[i] - '0' ) * dig;
            dig *= 10;
        }

        int t = n;
        while( t > 0 ) {
            ++cnt[t % 10];
            t /= 10;

        }
        for( int i = 0; i < 10; ++i ) {
            if( cnt[i] == 4 ) {
                  flg = false;
                  break;

            }
        }

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

        }


        while( n != 6174 ) {
            int l = 0, s = 0;
            vector< int > tmp;
            /*while( n > 0 ) {
                tmp.pb( n % 10 );
                n /= 10;

            }*/

            tmp.pb( n % 10 ); n /= 10;
            tmp.pb( n % 10 ); n /= 10;
            tmp.pb( n % 10 ); n /= 10;
            tmp.pb( n % 10 ); n /= 10;
            sort( tmp.begin(), tmp.end() );
            int dig = 1;
            int pos = 0;
            while( pos < tmp.size() ) {
               s += tmp[pos] * dig;
               dig *= 10;
               ++pos;

            }
            pos = tmp.size() - 1;
            dig = 1;
            while( pos >= 0 ) {
                l += tmp[pos] * dig;
                dig *= 10;
                --pos;

            }
            n = s - l;
            ++res;

        }
        cout << res << endl;

    }
    return 0;
}