Go: package main

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

var memo map[int]int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if _, ok := memo[v]; !ok {
		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(map[int]int)
	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<sstream>
#include<cmath>
#include<algorithm>
#include<cassert>
using namespace std;


bool allsame(string s){
  char c=s[0];
  for(int i=0;i<s.size();i++){
    if(s[i]!=c)return 0;
  }
  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){
      string tmp_n=toSortString(n);
      if(allsame(tmp_n)){
        cout<<"NA"<<endl;
        break;
      }
      ans++;
      int l,s;
      //cout<<tmp_n<<endl;
      l=make_l(tmp_n);
      s=make_s(tmp_n);
      //cout<<l<<endl;
      //cout<<s<<endl<<endl;
      n=l-s;
      //cout<<n<<endl;
      assert(ans<100);
    }
    if(n==6174){
      cout<<ans<<endl;
    }
  }
}

C++: #include<stdio.h>
int main(void)
{
	int n,i,w,p[10],h[10],flg,c,g[10],j;
	while(1){
		flg=0;
		c=0;
	scanf("%d",&n);
	if(n==0000)	break;
	while(1){
		if(n==6174) break;
		if(n<1000){
			h[0]=0;
		}
		else{
			h[0]=n/1000;
		}
		if(n<100){
			h[1]=0;
		}
		else{
			h[1]=(n%1000)/100;
		}
		if(n<10){
			h[2]=0;
		}
		else{
			h[2]=((n%1000)%100)/10;
		}
		h[3]=(((n%1000)%100)%10)/1;
		if(h[0]==h[1]&&h[0]==h[2]&&h[0]==h[3]){
			flg=1;
			printf("NA\n");
			break;
		}
		c++;
		for(i=0;i<4;i++){
			p[i]=h[i];
			g[i]=h[i];
		}
		for(i=0;i<3;i++){
			for(j=i+1;j<4;j++){
				if(p[i]>p[j]){
					w=p[i];
					p[i]=p[j];
					p[j]=w;
				}
			}
		}
		for(i=0;i<3;i++){
			for(j=i+1;j<4;j++){
				if(g[i]<g[j]){
					w=g[i];
					g[i]=g[j];
					g[j]=w;
				}
			}
		}
		n=(g[0]*1000+g[1]*100+g[2]*10+g[3])-(p[0]*1000+p[1]*100+p[2]*10+p[3]);
	}
		if(flg!=1){
			printf("%d\n",c);
		}
	}
	return 0;
}

C++: #include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
	int a,s,d[4],f,g,h,i,j,kai,high,low;
	while(1){
		scanf("%d",&a);
		if(a==0000){
			break;
		}
		kai=0;
		s=a;
		h=s;
		while(1){

			if(h==6174){
				printf("%d\n",kai);
				break;
			}
			for(i=0;i<4;i++){
				d[i]=s%10;
				s=s/10;
			}
			if(d[0]==d[1]&&d[1]==d[2]&&d[2]==d[3]){
				printf("NA\n");
				break;
			}
			sort(d,d+4);
			for(i=0;i<4;i++)
			//	printf("%d  ",d[i]);
			high=d[3]*1000+d[2]*100+d[1]*10+d[0];
			 low=d[0]*1000+d[1]*100+d[2]*10+d[3];
			h=high-low;
			kai++;
			s=h;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int operate(int n)
{
  vector<int> v(4);
  for(int i = 0; i<4; i++){
    v[i] = n % 10;
    n /= 10;
  }
  int L = 0,S = 0;
  sort(v.begin(),v.end());
  for(int i=0; i<4; i++){
    L *= 10; L += v[3-i];
    S *= 10; S += v[i];
  }
  return L - S;
}
int main()
{
  for(;;){
    int n;
    cin >> n;
    if(n == 0) break;
    if(n == 6174){
      cout << 0 << endl;
      continue;
    }
    if(n % 1111 == 0){
      cout << "NA" << endl;
      continue;
    }
    int ret = 1;
    for(;;){
      n = operate(n);
      if(n == 6174) break;
      ret++;
    }
    cout << ret << 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"){
		for(int i=0;i<4-n.size();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"){
				ans++;
				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;
				tmp=1000*b[0]+100*b[1]+10*b[2]+b[3];
				tmp-=1000*a[0]+100*a[1]+10*a[2]+a[3];
				n=itos(tmp);
				//cout<<n<<endl;
			}
		}
		if(flag)cout<<ans<<endl;
		else cout<<"NA"<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a,b) (a<b?(a=b,1):0)
#define chmin(a,b) (a>b?(a=b,1):0)
#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

int main() {
  string n;
  while(cin>>n, n!="0000") {
    if (n[0]==n[1]&&n[0]==n[2]&&n[0]==n[3]) {
      puts("NA");
      continue;
    }
    int res = 0;
    while(1) {
      if (n == "6174") break;
      string tmp = n;
      sort(ALL(tmp),greater<int>());
      int L = atoi(tmp.c_str());
      sort(ALL(tmp));
      int S = atoi(tmp.c_str());
      int nxt = L-S;
      char hoge[10];
      sprintf(hoge,"%04d",nxt);
      n = string(hoge);
      // cout << n << endl;
      res++;
    }
    cout << res << endl;
  }
}

C++: #include<iostream>
#include<string>
#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;
}


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;
		}
		int ans=0;;
		while(s!="6174"){
			ans++;
			int a,b;
			sort(s.begin(),s.end());
			a=to_i(s);
			sort(s.begin(),s.end(),greater<char>());
			b=to_i(s);
			s=to_s(b-a);
		}
		cout<<ans<<endl;
		
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int n;
	int ans;
	int d[4];
	int tmp,big,sma;
	
	while(cin>>n,n){
		if(n%1111==0)cout<<"NA"<<endl;
		else{
			ans=0;
			while(n!=6174){
				tmp=n;
				sma=big=0;
				for(int i=0;i<4;i++){
					d[i]=tmp%10;
					tmp/=10;
				}
				sort(d,d+4);
				for(int i=0;i<4;i++){
					sma*=10;
					sma+=d[i];
				}
				for(int i=3;i>=0;i--){
					big*=10;
					big+=d[i];
				}
				n=big-sma;
				ans++;
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}