Go: package main

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

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 {
		var s string
		fmt.Scan(&s)
		v, _ = strconv.Atoi(s)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int n,N;
	while((cin>>n)){
		if(n==0)return 0;
		int l2[4],s2[4],c=0;
		while(n!=6174){
			int L=0,S=0;
			l2[0]=n/1000;
			n%=1000;
			l2[1]=n/100;
			n%=100;
			l2[2]=n/10;
			l2[3]=n%10;
			sort(l2,l2+4);
			s2[0]=l2[3];
			s2[1]=l2[2];
			s2[2]=l2[1];
			s2[3]=l2[0];
			int j=0;
			if(l2[0]==l2[1]&&l2[1]==l2[2]&&l2[2]==l2[3]){
			cout<<"NA"<<endl;
			goto masatoy;
			}
			for(int i=1000;i>=1;i/=10){
				L+=l2[j]*i;
				S+=s2[j]*i;
				j++;
			}
//cout<<c<<"?????????"<<n<<endl;
			n=S-L;
			c++;
		}
		cout<<c<<endl;
masatoy:;
	}
}

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

int main()
{
  while (true) {
    string n;
    bool f = false;
    int cnt = 0;
    cin >> n;

    if (n == "0000") {
      break;
    }
    for (int i = 1; i < 4; i++) {
      if (n[0] != n[i]) f = true;
    }
    if (!f) {
      cout << "NA" << endl;
      continue;
    };

    while (n != "6174") {
      vector<char> a, b;
      for (int i = 0; i < 4; i++) {
        a.push_back(n[i]);
      }
      b = a;

      sort(a.begin(), a.end());
      sort(b.begin(), b.end(), greater<char>());

      string hayami = "";
      string kanade = "";
      for (int i = 0; i < 4; i++) {
        hayami += a[i];
        kanade += b[i];
      }

      int tmp_a, tmp_b;
      sscanf(hayami.c_str(), "%d", &tmp_a);
      sscanf(kanade.c_str(), "%d", &tmp_b);

      n = to_string(tmp_b - tmp_a);

      cnt++;
    }
    
    cout << cnt << endl;
  }
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
  char n[5],a[5],b[5];
  int sum,i;
  while(scanf("%s",n),strcmp(n,"0000")!=0){
    
    if(n[0]==n[1]&&n[1]==n[2]&&n[2]==n[3]){
      puts("NA");
      continue;
    }
    
    for(i=0;i<1000;i++){
      
      if(strcmp(n,"6174")==0){
	printf("%d\n",i);
	break;
      }
      
      strcpy(a,n);
      sort(a,a+4);
      reverse(a,a+4);
      strcpy(b,n);
      sort(b,b+4);
      sum = atoi(a)-atoi(b);
      
      n[0] = sum/1000 + '0';
      n[1] = sum/100%10 + '0';
      n[2] = sum/10%10 + '0';
      n[3] = sum%10 + '0';
    }
    
  }
  return(0);
}

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

string i2s(int num){
  stringstream ss;
  string str;
  ss << num;
  ss >> str;
  return str;
}
int main(){
  char num[4];

  while(1){
    for(int i = 0 ; i < 4 ; i++)cin >> num[i];
    if(num[0] == '0' && num[1] == '0' && num[2] == '0' && num[3] == '0')break;
    bool f = false;
    for(int i = 0 ; i < 3 ; i++){
      if(num[i] != num[i+1])f = true;
    }

    if(!f){
      cout << "NA" << endl;
      continue;
    } 

    int cnt = 0,t = 0;
    if(strcmp(num,"6174") == 1)t = 1;
    while(strcmp(num,"6174") != t ){
      cnt++; 
      sort(num,num + 4);
      int s = atoi(num);
      sort(num,num + 4,greater<int>());
      int l = atoi(num);
      strcpy(num,i2s(l-s).c_str());
    }
    cout << cnt << endl;
  }
  return 0;
}

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