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 <vector>
#include <algorithm>
#include <sstream>
#include <cstdlib>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int main()
{
	string s;
	while(cin >> s)
	{
		if(s == "0000") break;
		
		bool flag = true;
		rep(i,s.size()) if(s[0] != s[i]) flag = false;

		if(flag)
		{
			cout << "NA" << endl;
		}
		else
		{
			int cnt = 0;
			while(true)
			{
				if(s == "6174")
				{
					cout << cnt << endl;
					break;
				}
			
				string temp = s,temp2 = s;
				sort(temp.begin(),temp.end());
				sort(temp2.begin(),temp2.end(),greater<char>());

				int t = atoi(temp.c_str());
				int t2 = atoi(temp2.c_str());

				stringstream ss;
				ss << t2 - t;

				s = "";
				REP(i,ss.str().size(),4) s += "0";

				s += ss.str();
				cnt++;
			}
		}

	}
}

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

int main() {
	int N, x = 0;
	vector<int> n(4);
	vector<int> m(4);

	cin >> N;
	n.at(0) = N / 1000;
	n.at(1) = (N - n.at(0) * 1000) / 100;
	n.at(2) = (N - n.at(0) * 1000 - n.at(1) * 100) / 10;
	n.at(3) = N - n.at(0) * 1000 - n.at(1) * 100 - n.at(2) * 10;

	for (;;){
		sort(n.begin(), n.end());

		for (int i = 0; i < 4; i++)
			m.at(i) = n.at(i);

		reverse(m.begin(), m.end());

		for (int i = 0; i < 4; i++)
			n.at(i) = m.at(i) - n.at(i);

		x++;

		if (n.at(0) == 6 && n.at(1) == 1 && n.at(2) == 7 && n.at(3) == 4){
			cout << x;
			break;
		}
	}
}

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

template <class F, class T>
void convert(const F &f, T &t){
	stringstream ss;
	ss << f;
	ss >> t;
}

int main(){
	int x, a, b;
	string s;
	while(scanf("%d", &x), x){
		if(x % 1111 == 0){
			puts("NA");
		}
		else{
			int ans;
			for(ans = 0; x != 6174; ++ans){
				convert(x, s);
				sort(s.begin(), s.end());
				convert(s, a);
				reverse(s.begin(), s.end());
				convert(s, b);
				x = b - a;
			}
			printf("%d\n", ans);
		}
	}
}

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

main(){
	int n ;
	while( cin >> n , n ){
		int p=1000 , cnt=0 ;
		int c[4] = {} ;
		for( int i=0 ; i<4 ; i++ ){
			c[i] = n/p ;
			n -= c[i] * p ;
			p /= 10 ;
		}
		n = 0 ;
		for( int i=0 ; i<4 ; i++ ){
			if( c[0] == c[i] ) cnt++ ;
			n += c[3-i] * pow(10,i) ;
		}
		
		if( cnt == 4 ) cout << "NA" << endl ;
		else {
			cnt = 0 ;
			while(n-6174){
				sort(c,c+4) ;
				int mx=0 , mn=0 ;
				for( int i=0 ; i<4 ; i++ ) mx += c[i] * pow(10,i) ;
				for( int i=0 ; i<4 ; i++ ) mn += c[3-i] * pow(10,i) ;
				n = mx - mn ;
				p = 1000 ;
				for( int i=0 ; i<4 ; i++ ){
					c[i] = n/p ;
					n -= n/p * p ;
					p /= 10 ;
				}
				n = mx - mn ;
				cnt++ ;
			}
			cout << cnt << endl ;
		}
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <numeric>
#include <algorithm>

using namespace std;

int main(int argc, char **argv) {
  while (true) {
    char s[4];
    cin >> s;
    if (strcmp(s, "0000") == 0) {
      break;
    } else {
      int n = atoi(s), count = 0, a, b;

      while (true) {
        if (n == 6174) {
          cout << count << endl;
          break;
        } else if (s[0] == s[1] && s[0] == s[2] &&
                   s[0] == s[3]) {
          cout << "NA" << endl;
          break;
        }
        sort(s, s + 4,  greater<char>());
        //cout << s << endl;                                                                                                                                              
        a = atoi(s);
        //cout << a << endl;                                                                                                                                              
        sort(s, s + 4);
        //cout << s << endl;                                                                                                                                              
        b = atoi(s);
        //cout << b << endl;                                                                                                                                              
        n = a - b;
        count++;
        stringstream ss;
        ss << n;
        strncpy(s, ss.str().c_str(), 4);
      }
    }
  }

  return 0;
}

C++: #include<algorithm>
#include<iostream>
#include<iomanip>
#include<sstream>
#include<string>
#include<cstdlib>

int main()
{
	std::string N;
	while( std::cin >> N, N != "0000" )
	{
		int cnt = 0;

		bool f = true;
		for( int i = 0; i != 3; ++i )
			f &= N[i] == N[i+1];

		if( f )
			std::cout << "NA" << std::endl;
		else
		{
			while( N != "6174" )
			{
				std::string L = N;
				std::sort( L.rbegin(), L.rend() );
				
				std::string S = L;
				std::reverse( S.begin(), S.end() );
				
				std::ostringstream oss;
				oss << std::setw( 4 ) << std::setfill( '0' ) << atoi( L.c_str() ) - atoi( S.c_str() );
				N = oss.str();

				++cnt;
			}

			std::cout << cnt << std::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 <cstdio>
#include <algorithm>
#include <functional>
using namespace std;

int main()
{
    int num[4];
    int l, s;
    int n;

    scanf("%1d%1d%1d%1d", &num[0], &num[1], &num[2], &num[3]);
    while (num[0] != 0 || num[1] != 0 || num[2] != 0 || num[3] != 0) {
        
        if (num[0] == num[1] && num[1] == num[2] && num[2] == num[3]) {
            printf("NA\n");
        } else {
            int ans = 0;
            n = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];
            
            while (n != 6174) {
                sort(num, num + 4, greater<int>());
                l = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];
                sort(num, num + 4);
                s = num[0] * 1000 + num[1] * 100 + num[2] * 10 + num[3];

                n = l - s;
                num[0] = n / 1000;
                num[1] = (n / 100) % 10;
                num[2] = (n / 10 ) % 10;
                num[3] = n % 10;


                ans++;
            }

            printf("%d\n", ans);
        }

        scanf("%1d%1d%1d%1d", &num[0], &num[1], &num[2], &num[3]);
    }
    
    return 0;
}