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]
}

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<bits/stdc++.h>
using namespace std;
int main(void){
	while(1){
		int n;
		cin >> n;
		if(!n) break;
		if(n == 6174) cout << 0 << endl;
		else {
			int as[4], de[4];
			int c = 1, flg;
			while(1){
				for(int i = 0; i < 4; i++){
					int t = n % 10;
					as[i] = t; de[i] = t;
					n /= 10;
				}
				flg = 1;
				for(int i = 1; i < 4; i++){
					if(as[i] != as[i - 1]) flg = 0;
				}
				if(flg){
					cout << "NA" << endl;
					break;
				}
				sort(as, as + 4);
				sort(de, de + 4, greater<int>());
				int a = 0, d = 0;
				for(int i = 0; i < 4; i++){
					a *= 10; d *= 10;
					a += as[i]; d += de[i];
				}
				if(d - a == 6174) break;
				else n = d - a, c++;
			}
			if(flg != 1) cout << c << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
int stoi(string a){
		int b;
		stringstream ss;
		ss<<a;
		ss>>b;
		return b;
	}
	string itos(int a){
		stringstream ss;
		ss <<a;
		string b;
		b=ss.str();
		return b;
	}

int main(){
	string n;int t;
	while(cin>>n,n!="0000"){
		int ns=n.size();
		for(int i=0;i<4-ns;i++)n="0"+n;
		int ans=0;
		bool flag=true;
		if(n[0]==n[1]&&n[1]==n[2]&&n[2]==n[3])flag=false;
		
		if(flag){
			while(n!="6174"){
			//	cout<<n<<endl;
				//cin>>t;
				int a[4]={0},b[4]={0},ai,bi;
				for(int i=0;i<4;i++)a[i]=n[i]-'0',b[i]=n[i]-'0';
				sort(a,a+4);
				sort(b,b+4);
				reverse(b,b+4);
				int tmp=0,l,s;
				l=1000*b[0]+100*b[1]+10*b[2]+b[3];
				s=1000*a[0]+100*a[1]+10*a[2]+a[3];
			//	cout<<s<<" "<<l<<endl;
				tmp=l-s;
				n=itos(tmp);
				ns=n.size();
				for(int i=0;i<4-ns;i++)n="0"+n;
				ans++;
			}
		}
		if(flag)cout<<ans<<endl;
		else cout<<"NA"<<endl;
	}
	return 0;
}

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

int main(){
	string s;
	while(cin>>s){
		if(s=="0000")break;
		int ans=0;
		if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){
			ans=-1;
			goto FIN;
		}
		while(s=="6174"){
			string sa,sb;
			char ca[4]={s[0],s[1],s[2],s[3]};
			sort(ca,ca+4);
			for(int i=0;i<4;i++){
			sa.push_back(s[i]);	
			sb.push_back(s[3-i]);
			}
			int a=stoi(sa);
			int b=stoi(sb);
			int c=abs(a-b);
			
			string sc=to_string(c);
			for(int i=0;i<4-sc.size();i++)sc.insert(0,'0');
			s=sc;
			ans++;
			}
			FIN:
			if(ans==-1)cout<<"NA"<<endl;
			else cout<<ans<<endl;
		}
		return 0;
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
 
using namespace std;
 
#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
 
template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }
 
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int ton(string s){
  return atoi(s.c_str());
}
string tos(int N){
  stringstream ss;
  ss<<N;
  string s = ss.str();
  while(s.size() < 4) s = "0" + s;
  return s;
}
int solve(string s){
  if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) return -1;
  int ans = 0;
  while(true){
    if(s == "6174") break;
    sort(s.begin(), s.end());
    int S = ton(s);
    reverse(s.begin(), s.end());
    int L = ton(s);
    int N = L - S;
    s = tos(N);
    ans += 1;
  }
  return ans;
}
 
int main(){
  string s;
  while( cin>>s && s != "0000"){
    int ans = solve(s);
    if(ans == -1) cout<<"NA"<<endl;
    else cout<<ans<<endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
     int n,d[4],max,min,w[4]={1000,100,10,1};
     int c=0;
     cin >>n;
     while(n!=6174)
     {
       for(i=0;i<4;i++)
       {
             d[i]=n/w[i];
             n=n%w[i];
       }
       for(i=0;i<3;i++)
       {
          for(j=i+1;j<4;j++)
           {
                if(d[i]<d[j])
                 {
                    tmp=d[i];
                    d[i]=d[j];
                    d[j]=tmp;
                  }
            }
       }
        for(i=0;i<4;i++)
         {
              max=max+(d[i]*w[i]);
              min=min+(d[i]*w[3-i]);
         }
         n = max-min;
         c++;
     }
     cout << n << endl;
    return 0;
}

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){
  string res="0000";
  int tmp[4]={0,0,0,0};
  for(int i=0;n!=0;i++){
    tmp[i]=n%10;
    n/=10;
  }
  for(int i=0;i<4;i++){
    res[i]=tmp[i]+'0';
  }
  sort(res.begin(),res.end());
  return res;
}

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<100);
    }
    if(n==6174){
      cout<<ans<<endl;
    }
  }
}

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



vector<int> i_v(int n){
  vector<int> res;
  while(n!=0){
    res.push_back(n%10);
    n/=10;
  }
  return res;
}

int v_i(vector<int> v){
  int res=0;
  for(int i=0;i<v.size();i++){
    res*=10;
    res+=v[v.size()-1-i];
  }
  return res;
}

int main(){
  int n;
  vector<int> l,s;
  while(cin>>n){
    if(n=0)return 0;
    l = i_v(n);
    bool f = true;
    for(int i=1;i<l.size();i++){
      if(l[i-1]!=l[i]){
        f=false;
      }
    }
    if(f){
      cout<<"NA"<<endl;
      continue;
    }
    int ans=0;
    while(n!=6174){
      l.clear(),s.clear();
      l = i_v(n);
      sort(l.begin(),l.end());
      for(int i=0;i<l.size();i++){
        s.push_back(l[l.size()-1-i]);
      }
      n = v_i(l)-v_i(s);
      ans++;
    }
    cout<<ans<<endl;
  }
}