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

C++: #include <bits/stdc++.h>
using namespace std;
 
int main()
{
    string N;
    while ( cin >> N, N != "0000" ) {
        int cnt = 0;
        set < string > st;
 
        while ( N != "6174" && st.find(N) == st.end() ) {
            string L = N, S = N;
            int val = 0;
            st.insert(N);
            sort(rbegin(L), rend(L));
            sort(begin(S), end(S));
            val = atoi(L.c_str()) - atoi(S.c_str());
            N = to_string(val);
            while ( N.size() < 4 ) {
                N = '0' + N;
            }
            cnt++;
        }
 
        if ( N == "6174" ) {
            cout << cnt << endl;
        } else {
            cout << "NA" << endl;
        }
    }
 
    return ( 0 );
}

C++: #include<stdio.h>
int main(void)
{
	int n;
	int l,s;
	int i,j,temp;
	int keta[4];
	int cnt;
	
	scanf("%d",&n);
	
	while(n!=0000){
		if(n%1111==0){
			printf("NA\n");
			scanf("%d",&n);
		}
		cnt=0;
		while(n!=6174){
			for(i=0;i<4;i++){
				keta[i]=n%10;
				n=n/10;
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]<keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			l=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];

			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]>keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			s=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];
			n=l-s;
			cnt++;
		}
	printf("%d\n",cnt);
	scanf("%d",&n);
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(void){
	string s,ss,sss;
	int i,j;
	int x,y;
	int z;
	int flg;
	while(1){
		cin>>s;
		if(s=="0000")	break;
		flg=0;
		for(i=0;i<3;i++){
			if(s[i]!=s[i+1]){
				flg=1;
				break;
			}
		}
		if(flg==1){
			j=1;
			if(s=="6174")	j=0;
			else{
				while(1){
					if(s=="6174")	break;
					ss=s;
					sort(ss.begin(),ss.end(),greater<char>());
					if(ss=="6174")	break;
					sss=s;
					sort(sss.begin(),sss.end());
					if(sss=="6174")	break;
					z=1000;
					x=y=0;
					for(i=0;i<4;i++){
						x+=(ss[i]-'0')*z;
						y+=(sss[i]-'0')*z;
						z/=10;
					}
					//cout<<x<<" "<<y<<endl;
					z=x-y;
					//cout<<z<<endl;
					for(i=3;i>=0;i--){
						s[i]=(z%10)+'0';
						z/=10;
					}
					//cout<<s<<endl;
					if(s=="6174")	break;
					j++;
				}
			}
		}
		if(flg==1)	cout<<j<<endl;
		else 		cout<<"NA"<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
	while (1) {
		int cnt = 0;
		int n;
		cin >> n;
		if (n == 0000) {
			return 0;
		}
		bool b = true;
		if (n%1111==0) {
			cout << "NA" << endl;
			b = false;
		}
		while (b) {
			if (n == 6174) {
				break;
			}
			string s;
			s += n / 1000;
			n -= (n / 1000) * 1000;
			s += n / 100;
			n -= (n / 100) * 100;
			s += n / 10;
			s += n - 10 * (n / 10);
			sort(s.begin(), s.end());
			int L = 0, S = 0;
			L = (int)s[3] * 1000 + (int)s[2] * 100 + (int)s[1] * 10 + (int)s[0];
			S = (int)s[0] * 1000 + (int)s[1] * 100 + (int)s[2] * 10 + (int)s[3];
			n = L - S;
			cnt++;
		}
		if(b) cout << cnt << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<sstream>
#include<queue>
using namespace std;
int latte(string str){
    stringstream ss(str);
    int n;
    ss>>n;
    return n;
}
struct data{
    string str;
    int n;
    data(string a,int b):str(a),n(b){}
    data(){}
};
int main(){
    string str="";
    while(cin>>str,str!="0000"){
        int memo[10000];
        fill(memo,memo+10000,-1);
        queue<data>Q;
        Q.push(data(str,0));
        while(!Q.empty()){
            data ins=Q.front();Q.pop();
            str=ins.str;int n=ins.n;
            if(memo[latte(str)]!=-1)continue;
            memo[latte(str)]=n;
            string a,b;
            a=b=str;
            sort(a.rbegin(),a.rend());sort(b.begin(),b.end());
            int to=latte(a)-latte(b);
            stringstream ss;
            ss<<to;
            string to_;
            ss>>to_;
            Q.push(data(to_,n+1));
        }
        if(memo[6174]==-1)cout<<"NA"<<endl;
        else cout<<memo[6174]<<endl;
    }
}

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

int main(){
	int num[4],l,s,aa=0;
	string n;
	while(cin>>n,n!="0000"){
		/*if(n>999){
			num[0]=n/1000;
			num[1]=n/100%10;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n>99&&n<1000){
			num[0]=0;
			num[1]=n/100%10;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n>9&&n<100){
			num[0]=0;
			num[1]=0;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n<10){
			num[0]=0;
			num[1]=0;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		*/
		for(int i=0;i<4;i++)num[i]=n[i]-'0';
		aa=0;
		if(num[0]==num[1]&&num[1]==num[2]&&num[2]==num[3]){
			cout<<"NA"<<endl;
			continue;
		}
		while(num[0]!=6&&num[1]!=1&&num[2]!=7&&num[3]!=4){
			l=0,s=0;
			sort(num,num+4);
			for(int i=0;i<4;i++)cout<<num[i];
			cout<<endl;
			s=num[0]*1000+num[1]*100+num[2]*10+num[3];
			l=num[3]*1000+num[2]*100+num[1]*10+num[0];
			num[0]=(l-s)/1000;
			num[1]=(l-s)/100%10;
			num[2]=(l-s)/10%10;
			num[3]=(l-s)%10;
			aa++;
		}
		cout<<aa<<endl;
	}
}

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

using namespace std;

template<class T, class U> T lexical_cast(const U &from) {
    stringstream ss;
    ss << from;
    T to;
    ss >> to;
    return to;
}

bool same(const string &s) {
    return s[0] == s[1] && s[0] == s[2] && s[0] == s[3];
}

int main() {
    for(string S; cin >> S, S != "0000"; ) {
        if(same(S)) {
            cout << "NA" << endl;
            continue;
        }
        int ans = 0;
        while(S != "6174") {
            sort(begin(S), end(S));
            string L = "    ";
            for(int i = 0; i < 4; i++) L[i] = S[3 - i];
            S = lexical_cast<string>(lexical_cast<int>(L) - lexical_cast<int>(S));
            ans++;
        }
        cout << ans << endl;
    }
}