Go: package main

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

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		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))
		memo[v] = solve(l-s) + 1
	}
	return memo[v]
}

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


int to_i(string &s){
	int a=0;
	for(int i=0;i<s.size();i++)a=a*10+s[i]-'0';
	return a;
}
string to_s(int n){
	string s;
	while(n){
		s+=n%10+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}



map<int,int> cache;

int solve(int n){
	string s=to_s(n);
	if(s=="6174")return 0;
	int a,b;
	sort(s.begin(),s.end());
	a=to_i(s);
	sort(s.begin(),s.end(),greater<char>());
	b=to_i(s);
	return cache[n] = 1+solve(b-a);
}


int main(){
	string s;
	while(cin>>s&&s!="0000"){
		if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){
			cout<<"NA\n";
			continue;
		}
		cout<<solve(to_i(s))<<endl;
		
	}
	return 0;
}

C++: #include<iostream>
#include<cmath>
#include<vector>
#include<string>
#include<algorithm>
typedef unsigned long long ull;
#define rep(i,a) for(int i=0;i<a;i++)
#define loop(i,a,b) for(int i=a;i<b;i++)
using namespace std;
const double eps = 1e-10;
const double pi = acos(-1);
const double inf = (int)1e8;

string itos(int s){
  string res="0000";
  rep(i,4){
    res[res.size()-i-1] = s%10 + '0';
    s /= 10;
  }
  return res;
}
int stoi(string s){
  int res = 0;
  int w = 1000;
  rep(i,4){
    //cout<<(int)(s[i]-'0')<<" "<<w<<endl;
    res += (s[i] - '0') * w;
    w /= 10;
  }
  return res;
}

int main(void){
  string s;
  while(cin>>s){
    bool brek = true;
    rep(i,4)if(s[i] != s[0])brek = false;
    if(brek&&s[0]=='0')break;
    if(brek&&s[0]!='0'){cout<<"NA"<<endl;continue;}
        
    int cnt = 0;
    while(s!="6174"){
      cnt ++;
      vector<char> used;
      rep(i,4){
	used.push_back(s[i]);
      }
    
      sort(used.begin(),used.end());
      string min,max;
      rep(i,used.size()){
	max += used[used.size()-i-1];
	min += used[i];
      }
      int maxv=stoi(max),minv=stoi(min);
      int diffv = maxv - minv;
      s = itos(diffv);
    }
    cout<<cnt<<endl;
  }
  return 0;
}

C++: 
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<numeric>

using namespace std;

typedef long long ll;

int main()
{
	while (1) {
		string s;
		cin >> s;
		if (s == "0000")break;
		else if ((s[0] == s[1])&&(s[1]==s[2])&&(s[2]==s[3])) cout << "NA" << endl;
		else if (s == "6174")cout << 0 << endl;
		else {
			ll cnt = 0;
			while (1) {
				int d[4];
				d[0] = s[0]-'0', d[1] = s[1]-'0', d[2] = s[2]-'0', d[3] = s[3]-'0';
				sort(d,d+4);
				int S = d[0] * 1000 + d[1] * 100 + d[2] * 10 + d[3];
				reverse(d,d+4);
				int L = d[0] * 1000 + d[1] * 100 + d[2] * 10 + d[3];
				cnt++;
				int res = L - S;
				if (res == 6174)break;
				s = to_string(res);
				while (s.size() < 4) {
					s = '0' + s;
				}
			}
			cout << cnt << endl;
		}
	}
	return 0;
}

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 cnt;
    int n;
    int l[4], s[4];
    int l_num, s_num;

    while ( true ) {
        cnt = 0;
        scanf("%d", &n );
        if ( n == 0 ) break;
        if ( n % 1111 == 0 ) {
            printf("NA\n");
        } else {
            while( n != 6174 ) {
                l_num = s_num = 0;

                for ( int i = 3; i >= 0; --i ) {
                    s[i] = n % 10;
                    n /= 10;
                }

                sort( s, s + 4 );

                s_num += s[0] * 1000;
                s_num += s[1] * 100;
                s_num += s[2] * 10;
                s_num += s[3];

                l_num += s[3] * 1000;
                l_num += s[2] * 100;
                l_num += s[1] * 10;
                l_num += s[0];

                n = l_num - s_num;
                cnt++;
            }
            printf("%d\n", cnt );
        }
    }

    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<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
int main(){
  string s;
  while(cin>>s,s!="0000"){
    int co=0;
    if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){cout<<"NA"<<endl;continue;}
    while(s!="6174"){
      vector<int>tmp;
      rep(i,4)tmp.pb(s[i]-'0');
      sort(all(tmp));
      int a=0,b=0;
      rep(i,4){
	a+=pow(10,3-i)*tmp[i];
	b+=pow(10,i)*tmp[i];
      }
      a=b-a;
      co++;
      s="";
      while(a){
	s=(char)(a%10+'0')+s;
	a/=10;
      }
      while(s.size()!=4)s='0'+s;
    }
    cout<<co<<endl;
  }
}

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;
	int ans = 0;
	cin >> s;
	if(s == "0000") break;
	else if(s[0] == s[1] && s[1] == s[2] && s[2] == 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 u = 0;
	    vector<int> c(4);
	    RREP(i,4){
		if(a[i] < b[i]) {
		    if(a[i-1] > 0){
			a[i-1]--;
			a[i] += 10;
		    }else if(a[i-2] > 0){
			a[i-2]--;
			a[i-1] = 9;
			a[i] += 10;
		    }else{
			a[i-2] = 9;
			a[i-1] = 9;
			a[i] += 10;
		    }
		}
		c[i] = a[i] - b[i];
	    }
	    s = to_string(1000 * c[0] + 100 * c[1] + 10 * c[2] + c[3]);
	    ans++;
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int stoi(string s){
	int size=s.size(),res=0;
	for(int i=size-1;i>=0;i--){
		res+=(int)(s[i]-'0')*pow(10,size-1-i);
	}
	return res;
}

string itos(int n){
	int size=log(n)/log(10)+1;
	string s="";
	rep(i,size){
		s+=(char)(n/pow(10,size-1-i)+'0');
		n%=(int)pow(10,size-1-i);
	}
	if(size<4)rep(i,4-size)s='0'+s;
	return s;
}

int main(){
	string s,res[2];
	int ma,mi;
	while(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;
		}
		int cnt=0;
		while(s!="6174"){
			cnt++;
			sort(s.begin(),s.end());
			mi=stoi(s);
			reverse(s.begin(),s.end());
			ma=stoi(s);
			s=itos(ma-mi);
		}
		cout<<cnt<<endl;
	}
	return 0;
}