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]
	return solve(l-s) + 1
}

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 <bits/stdc++.h>
using namespace std;

int main(){
  string n;
  while(1){
    cin >> n;
    if(n == "0000") break;
    int num[4];
    for(int i = 0;i < 4;i++) num[i] = n[i] - '0';

    if(n == "6174"){
      cout << 0 << endl;
    }else if(num[0] == num[1] && num[1] == num[2] && num[2] == num[3]){
      cout << "NA" << endl;
    }else{


      int cnt = 1;
      int tmpa,tmpb,tmpc;
      while(1){
        tmpa = tmpb = 0;
        sort(num,num+4);
        int tmp = 1;
        for(int i = 0;i < 4;i++){
          tmpa += num[i] * tmp;
          tmp *= 10;
        }
        tmp = 1;
        for(int i = 0;i < 4;i++){
          tmpb += num[3-i] * tmp;
          tmp *= 10;
        }
        tmpc = tmpa - tmpb;
        if(cnt == 10) break;
        if(tmpc == 6174){
          cout << cnt << endl;
          break;
        }
        for(int i = 0;i < 4;i++){
          num[i] = tmpc % 10 ;
          tmpc /= 10;
        }
        cnt++;
      }
    }
  }

  return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int n,h[10],s[10],k[10],i,j,temp,flg,cnt;
	while(1){
		flg=0;
		cnt=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;
			}
			cnt++;
//			printf("%d\n",cnt);
			for(i=0;i<4;i++){
				s[i]=h[i];
				k[i]=h[i];
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(s[i]>s[j]){
						temp=s[i];
						s[i]=s[j];
						s[j]=temp;
					}
				}
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(k[i]<k[j]){
						temp=k[i];
						k[i]=k[j];
						k[j]=temp;
					}
				}
			}
			n=(k[0]*1000+k[1]*100+k[2]*10+k[3])-(s[0]*1000+s[1]*100+s[2]*10+s[3]);
		}
		if(flg!=1){
			printf("%d\n",cnt);
		}
	}
	return 0;
}

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

void ssort(string& s,bool _greater){
    for(int i=0;i<4;i++){
        for(int j=i+1;j<4;j++){
            if(_greater){
                if(s[i]>s[j]){
                    swap(s[i],s[j]);
                }
            }
            else{
                if(s[i]<s[j]){
                    swap(s[i],s[j]);
                }
            }
        }
    }
}

int main() {

    string s;

    while (1) {

        cin >> s;

        if(s[0]==s[1]&&s[1]&&s[2]&&s[2]==s[3]&&s[3]==s[0]){
            if(s[0]=='0'){
                break;
            }
            cout << "NA" << endl;
            continue;
        }

        int ans = 0;

        while(1) {


            if (s == "6174") {
                cout << ans << endl;
                break;
            }

            ans++;

            string s1, s2;

            s1 = s2 = s;

            //sort(s1.begin(),s1.end());
            //sort(s2.begin(),s2.end(),greater<string>());

            ssort(s1, false);
            ssort(s2, true);

            int a1, a2;

            stringstream ss;

            ss << s1;
            ss >> a1;
            ss.clear();
            ss << s2;
            ss >> a2;
            a1 -= a2;

            ss.clear();

            ss << a1;

            ss >> s;

            if(s.size() != 4){
                while(s.size()!=4){
                    s = '0' + s;
                }
            }
        }

    }

    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<sstream>
#include<algorithm>
#define all(n) n.begin(),n.end()
using namespace std;

string itos(int i)
{
  stringstream ss;
  ss << i;
  return ss.str();
}

void EXAM(string N)
{
  int cnt = 0;
  while(true)
    {
      vector<int> mex,men;
      for(int i=0;i<N.size();i++)
	mex.push_back(N[i]-'0');
      sort(all(mex),greater<int>());
      men = mex;
      sort(all(men));
      
      int imex,imen;
      imex = imen = 0;
      for(int i=3;i>=0;i--)
	{
	  imex += mex[i]*pow(10,3-i);
      imen += men[i]*pow(10,3-i);
	}

      string pre = itos(imex-imen);      
      N = string(4-pre.size(),'0') + pre;
      //cout << "L = " << imex << " S = " << imen  << " L-S = " << N << endl;
      cnt++;
      if(N == "6174")
	break;
    }
  cout << cnt << endl;
}

int main()
{
  string N;
  while(true)
    {
      cin >> N;
      if(N == "0000")
	break;
      if(N == "1111" || N == "2222" || N == "3333" || N == "4444" || N == "5555" || N == "6666" || N == "7777" || N == "8888" || N == "9999")
	{
	  cout<< "NA" << endl;
	  continue;
	}
      if(N == "6174")
	{
	  cout << 0 << endl;
	  continue;
	}
      EXAM(N);

    }
  return 0;
}

C++: // 2015/07/21 Tazoe

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	while(true){
		int N;
		cin >> N;

		if(N==0)
			break;

		if(N%1111==0){			// 全桁が同じ数字
			cout << "NA" << endl;
			continue;
		}

		int cnt = 0;

		while(N!=6174){
			int n[4];
			n[0] = N/1000;		// 千の位
			n[1] = N/100%10;	// 百の位
			n[2] = N/10%10;		// 十の位
			n[3] = 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 <bits/stdc++.h>
using namespace std;

typedef long int lint;
typedef long long int llint;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
typedef pair<long, long> pll;

#define EACH(i, c) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define FOR(i, s, e) for (int i = (s); i < (int)(e); ++i)
#define REP(i, n) FOR(i, 0, n)
#define endl '\n'
#define mod 1000000007
#define INF 100000000
#define fk first
#define sv second

const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[] = {0, 1, 0, -1, 1, -1, 1, -1};

template<typename T>
void print(const std::vector<T>& v, char sep=' ') {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i != 0) cout << sep;
    cout << v[i];
  }
  cout << endl;
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;

  while (cin >> n){
    int ct = 0;
    if (n % 1111 == 0){
      cout << "NA\n";
      continue;
    }
    while (n != 6174){
      int l, s, num[4];
      REP(i, 4){
        num[i] = n % 10;
        n /= 10;
      }
      sort(n, n + 4);
      l = s = 0;
      REP(i, 4){
        s = n * 10 + num[3 - i];
        l = n * 10 + num[i];
      }
      n = l - s;
      ct++;
    }
    cout << ct << endl;
  }
}

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

int main()
{
    
    while(true){
        int N;
        cin>>N;
        
        if(N==0){
            break;
        }
        if(N%1111==0){
            cout<<"NA"<<endl;
            continue;
        }
        
        int cnt = 0;
        
        while(N!=6174){
            int n[4];
            n[0] = N/1000;
            n[1] = N/100%10;
            n[2] = N/10%10;
            n[3] = 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;
}