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

int to_i(const string &s) {
    int n;
    istringstream is(s);
    is >> n;
    return n;
}

string to_s(int n) {
    char buf[256];
    sprintf(buf, "%04d", n);
    return string(buf);
}

int main() {
    string s;
    while (cin >> s, s != "0000") {
        string t;
        int n;
        int c = 0;
        while (true) {
            if (s == "6174") {
                cout << c << endl;
                break;
            }
            t = s;
            sort(s.begin(), s.end());
            sort(t.begin(), t.end(), greater<char>());
            if (s == t) {
                cout << "NA" << endl;
                break;
            }
            n = to_i(t) - to_i(s);
            s = to_s(n);
            c++;
        }
    }
    return 0;
}

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

using namespace std;

int main(){
  string s;
  int l[4], r[4], m[4];
  while(1){
    cin >> s;
    if(s=="0000") break;
    if(s[0]==s[1] && s[0]==s[2] && s[0]==s[3]){
      cout << "NA" << endl; continue;
    }
    m[0]=s[0]-'0', m[1]=s[1]-'0', m[2]=s[2]-'0', m[3]=s[3]-'0';
    for(int i=0; ; ++i){
      if(m[0]==6 && m[1]==1 && m[2]==7 && m[3]==4){
	cout << i << endl; break;
      }
      l[0]=m[0], l[1]=m[1], l[2]=m[2], l[3]=m[3];
      sort(l, l+4);
      r[0]=l[0], r[1]=l[1], r[2]=l[2], r[3]=l[3];
      reverse(r, r+4);
      int k = r[0]*1000 + r[1]*100 + r[2]*10 + r[3]
	- l[0]*1000 - l[1]*100 - l[2]*10 - l[3];
      m[0]=k/1000, k-=m[0]*1000;
      m[1]=k/100, k-=m[1]*100;
      m[2]=k/10, k-=m[2]*10;
      m[3]=k;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <functional>
#include <sstream>
#include <numeric>
#include <cstdio>
#include <cassert>
using namespace std;

string to_str(int n)
{
	string res;
	res.push_back('0' + n % 10000 /1000);
	res.push_back('0' + n % 1000 / 100);
	res.push_back('0' + n % 100 / 10);
	res.push_back('0' + n % 10 / 1);
	return res;
}

bool same_all(string s) { return count(s.begin(), s.end(), s[0]) == s.size(); }
int to_int(string s) { return atoi(s.data()); }

int main()
{
	for(string line; cin >> line && (line != "0000"); ) {
	  int times = 0;

		if(same_all(line)) {
			cout << "NA" << endl;
			continue;
		}
		
		for(; line != "6174"; times++) {
			sort(line.begin(), line.end());
			int small = to_int(line);
		
			reverse(line.begin(), line.end());
			int big = to_int(line);

			int next = big - small;

			line = to_str(next);
		}

		cout << times << endl;
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
	
int main(){
	while(1){
		int N,cnt;
		cin >>N;

		if(N==0000)
			break;

		if(N%1111==0){
			cout << "NA" <<endl;
			continue;
		}

		while(N!=6174){
			int n[4]={N/1000,N%1000/100,N%100/10,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<string>
#include<algorithm>
using namespace std;int main(){int n;while(true){cin>>n;if(n==0)break;if(n%1111==0)cout<<"NA"<<endl;else{int ans=0,count,s[4];while(true){if(n==6174)break;int a=10;for(int i=0;i<4;i++){s[i]=n%a/(a/10);a*=10;}sort(s,s+4);count=0;a=1;for(int i=0;i<4;i++){count+=a*s[i]-a*s[3-i];a*=10;}n=count;ans++;}cout<<ans<<endl;}}return 0;}

C++: #include<iostream>
using namespace std;
int main()
{
    int input;
    int digit[4];
    int max, min;
    int base[4]={1000,100,10,1};
    int c=0,i,j,tmp;

    cin >> input;
    while(input != 0)
    {
        if(input % 1111 == 0){
	    cout << "NA" << endl;
	    cin >> input;
	    continue;
        }
	while(input != 6174){
            for(i=0; i<4; i++) // 各桁を取り出す
            {
                digit[i]= input / base[i];
                input = input % base[i]; // Highestを除く
            }
            for(i=0;i<3;i++) // sort
            {
                for(j=i+1;j<4;j++)
                {
                     if(digit[i]<digit[j])
                    {
                        tmp=digit[i];
                        digit[i]=digit[j];
                        digit[j]=tmp;
                    }
                }
            }
	    max = min = 0;
            for(i=0;i<4;i++)
            {
                max += (digit[i]*base[i]);
                min += (digit[i]*base[3-i]);
            }
            input = max-min;
            c++;
	}
	//
        cout << c << endl;
	c = 0;
        cin >> input;
    }
    return 0;
}

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

const string ANS = "6174";
const int N = 4;

int main(int argc, char const* argv[])
{
	string buf;
	int c;
	int l[N], s[N], res[N];
	bool na;
	while(cin >> buf){
		c = 0;
		na = true;
		if(buf == "0000") break;
		for(int i(0);i < N - 1;++i){
			na = na && (buf[i] == buf[i + 1]);
		}
		if(na) {
			cout << "NA" << endl;
			continue;
		}
		while(buf != ANS){
			for(int i = 0;i < N;++i){
				l[i] = s[i] = (int)(buf[i] - '0');
			}
			sort(l, l + N, greater<int>());
			sort(s, s + N);
			for(int i = N - 1;0 <= i;--i){
				res[i] = l[i] - s[i];
				if(res[i] < 0){
					--l[i - 1];
					res[i] += 10;
				}
			}
			for(int i = 0;i < N;++i){
				buf[i] = (char)(res[i] + '0');
			}
			++c;
		}
		cout << c << endl;
		
	}
	return 0;
}

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

int main(void) {
	int S, L;
	string N;
	int ans;
	bool flag = false;

	while(1) {
flag=false;
		cin >> N;
		if(N[0] == N[1] && N[1] == N[2] && N[2] == N[3]) {
			flag = true;
			if(N[0] == '0')
				break;
		}

		if(!flag) {
			for(ans = 0; atoi(N.c_str()) != 6174; ans++) {
				sort(N.begin(), N.end());
				S = atoi(N.c_str());
				sort(N.begin(), N.end(), greater<int>());
				L = atoi(N.c_str());
				int tmp = L - S;

				for(int i = 1000, j = 0; i; i /= 10, j++) {
					N[j] = tmp / i + '0';
					tmp %= i;
				}
			}
		}

		if(flag)
			cout << "NA" << endl;
		else
			cout << ans << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string Int2Str(int);
int Str2Int(string);
int main(){
  int l,s,cal;
  string str,L,S;
  while(1){
    cin >> str;
    if(str=="0000") break;
    int cnt=0,flag=0;
    if(str=="6174") cnt--;
    while(1){
      cnt++;
      sort(str.begin(),str.end());
      S=str;
      reverse(str.begin(),str.end());
      L=str;
      l=Str2Int(L);
      s=Str2Int(S);
      cal=l-s;
      if(cal==6174){
	flag=1;
	break;
      }
      if(cal==0||cal==1111||cal==2222||cal==3333||cal==4444||cal==5555||cal==6666||cal==7777||cal==8888||cal==9999){
	break;
      }
      str=Int2Str(cal);
    }
    if(flag==1) cout << cnt << endl;
    else cout << "NA" << endl;
  }
  return 0;
}
string Int2Str(int n){
  int p;
  string ret;
  while(n){
    p=n%10;
    ret=(char)(p+'0')+ret;
    n/=10;
  }
  if(ret.size()!=4){
    while(ret.size()!=4){
      ret+='0';
    }
  }
  return ret;
}
int Str2Int(string str){
  int ret=0,i=0,x=1,len=4;
  while(len){
    ret=ret*x+(str[i]-'0');
    i++;
    x=10;
    len--;
  }
  return ret;
}