Go: package main

import (
	"fmt"
	"sort"
)

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]
	return solve(l-s) + 1
}

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<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int f(char* s){
  return (s[0]-'0')*1000+(s[1]-'0')*100+(s[2]-'0')*10+s[3]-'0';
}
int main(){
  int i;
  for(;;){
    char s[5];
    cin>>s;
    if(s[0]=='0'&&s[1]=='0'&&s[2]=='0'&&s[3]=='0')break;
    if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){
      cout<<"NA"<<endl;
      continue;
    }
    for(i=0;f(s)!=6174;++i){
      char t[5],u[5];
      memcpy(t,s,sizeof(s));
      sort(t,t+4);
      memcpy(u,s,sizeof(s));
      sort(u,u+4,greater<char>());

      int p=f(u)-f(t);
      s[0]=p/1000+'0';
      s[1]=p/100%10+'0';
      s[2]=p/10%10+'0';
      s[3]=p%10+'0';
    }
    cout<<i<<endl;
  }
  return 0;
}

C++: // 2013/07/07 Tazoe

#include <iostream>
#include <algorithm>
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){
			int n[4];
			n[0] = N/1000;
			n[1] = N/100%10;
			n[2] = N/10%10;
			n[3] = N%10;

			sort(n, n+4);

			int L = n[3]*1000+n[2]*100+n[1]*10+n[0];
			int S = n[0]*1000+n[1]*100+n[2]*10+n[3];

			N = L-S;
			cnt++;
		}

		cout << cnt << endl;
	}

	return 0;
}

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

int N,a[4],S,L,cnt;

int main(){
	while(1){
		cnt=0;
		cin>>N;
		if(N==0){break;}
		if(N%1111==0){
			cout<<"NA"<<endl;
			continue;
		}
		while(N!=6174){
			a[0]=N/1000;
			a[1]=(N%1000)/100;
			a[2]=(N%100)/10;
			a[3]=N%10;
			sort(a,a+4);
			L=a[3]*1000+a[2]*100+a[1]*10+a[0];
			S=a[0]*1000+a[1]*100+a[2]*10+a[3];
			N=L-S;
			cnt++;
		}
		cout<<cnt<<endl;
	}
	
	return 0;
}

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

int main(){

  bool d;
  string s;

  while(true){
    cin >> s;
    if(s == "0000") break;

    int ans = 0;
    while(s != "6174"){
      string a = s,b;

      d = true;
      for(int i=0;i<3;i++){
	if(s[i] != s[i+1]) d = false;
      }

      if(d) break;

      sort(a.begin(),a.end());
      b = a;
      reverse(b.begin(),b.end());

      int n = 0,m = 0;
      for(int i=0;i<4;i++){
	n *= 10;
	m *= 10;
	n += a[i] - '0';
	m += b[i] - '0';
      }

      m -= n;

      int k = 1000;
      s = "";

      for(int i=0;i<4;i++){
	s += (m/k + '0');
	m -= m/k * k;
	k /= 10;
      }

      ans++;
    }

    if(d) cout << "NA" << endl;
    else cout << ans << endl;

  }
}

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

typedef long long ll;

ll gcd( ll x, ll y );
ll lcm( ll x, ll y );

int main()
{
    int n;

    while ( true ) {
        scanf("%d", &n );
        if ( n == 0 ) break;
        if ( n % 1111 == 0 ) {
            printf("NA\n");
        } else if ( 6174 % n == 0 ) {
            printf("%d\n", 6174 / n - 1 );
        } else {
            printf("%d\n", 6174 / n );
        }
    }

    return 0;
}

ll gcd( ll x, ll y )
{
    if ( y == 0 ) return x;
    return gcd( y, x % y );
}

ll lcm( ll x, ll y )
{
    return x / gcd( x, y ) * y;
}

C++: #include <stdio.h>
#include <iostream>
#include <math.h>


using namespace std;




int main() {
	char a;
	int n[4];
	int kari;
	int counter;
	int l;
	int s;
	int count;
	while (1) {
		counter = 0;
		count = 0;
		for (int i = 0; i < 4; i++) {
			cin >> a;
			n[i] = a - '0';
			if (n[i] == 0) {
				counter++;
			}
		}
		if (counter == 4) {
			break;
		}

		if (n[0] == n[1] && n[1] == n[2] && n[2] == n[3]) {
			cout << "NA" << endl;
			continue;
		}
		
		while (1) {
			if (n[0] == 6 && n[1] == 1 && n[2] == 7 && n[3] == 4) {
				break;
			}
			for (int i = 3; i > 0; i--) {
				for (int j = 0; j < i; j++) {
					if (n[j] < n[j+1]) {
						kari = n[j];
						n[j] = n[j+1];
						n[j+1] = kari;
					}
				}
			}
			l = n[0] * 1000 + n[1] * 100 + n[2] * 10 + n[3];
			s = n[3] * 1000 + n[2] * 100 + n[1] * 10 + n[0];
			kari = l - s;
			for (int i = 0; i < 4; i++) {
				n[i] = kari / (int)pow(10,3-i);
				kari = kari % (int)pow(10,3-i);
			}
			count++;
		}
		cout << count << endl;
	}
	return 0;
}

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

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){
		if (n == "0000" || n == "0"){
			break;
		}
		if (n.length() < 4) {
			for (int i = 0; i < 4-n.length(); i++) {
				n += '0';
			}
		}
		if (n[0] == n[1] && n[0] == n[2] && n[0] == n[3]){
			cout << "NA" << endl;
			continue;
		}

		int cnt = 0;
		string N = n;
		while (1){
			if (N == "6174"){
				cout << cnt << endl;
				break;
			}
			// cout << N << endl;
			string L = N; sort(L.rbegin(), L.rend());
			string S = N; sort(S.begin(), S.end());
			int d = toInt(L) - toInt(S);
			N = toString(d);
			cnt++;
		}
	}

	return 0;
}

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.compare("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(RALL(a));
	    sort(ALL(b));
	    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>
#include <string>
#include <algorithm>
#include <deque>
using namespace std;
string L(string);
string S(string);
string sa(string,string);
int main(void)
{
	int count;
	string n;

	while(1){
		cin >> n;
		if(n=="0000") break;
		if(n=="1111"||n=="2222"||n=="3333"||n=="4444"||n=="5555"||n=="6666"||n=="7777"||n=="8888"||n=="9999") {
			cout << "NA" << endl;
			continue;
		}
		count=0;
		while(n!="6174"){
			n=sa(L(n),S(n));
			count+=1;
		}
		cout << count << endl;
	}
	return 0;
}
string S(string n)
{
	string s;
	deque<char> dn;
	for(int i=0;i<4;i++) dn.push_back(n[i]);
	sort(dn.begin(),dn.end());
	for(int i=3;0<=i;i--){
		s+=dn[i];
		dn.pop_back();
	}
	return s;
}
string L(string n)
{
	string l;
	deque<char> dn;
	for(int i=0;i<4;i++) dn.push_back(n[i]);
	sort(dn.begin(),dn.end());
	for(int i=0;i<4;i++){
		l+=dn[i];
	}
	return l;
}
string sa(string l,string s)
{
	string n;
	int lnum=0,snum=0,num,b=1;
	for(int i=3;0<=i;i--){
		lnum+=(b*(l[3-i]-'0'));
		snum+=(b*(s[3-i]-'0'));
		b*=10;
	}
	num=lnum-snum;
	for(int i=0;i<4;i++){
		b/=10;
		n+=((num/b)%10+'0');
	}
	//cout << n << endl;
	return n;
}