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

C++: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair<int,string> P;

static const double EPS = 1e-8;

bool is_all_same(string num){
  sort(num.begin(),num.end());
  for(int i=0;i+1<num.size();i++){
    if(num[i] != num[i+1]){
      return false;
    }
  }
  return true;
}

bool is_6174(const string& num){
  if(num[0] == '6'
     && num[1] == '1'
     && num[2] == '7'
     && num[3] == '4'){
    return true;
  }

  return false;
}

bool is_0000(const string& num){
  if(num[0] == '0'
     && num[1] == '0'
     && num[2] == '0'
     && num[3] == '0'){
    return true;
  }

  return false;
}

int main(){
  string LminusS;
  
  while(cin >> LminusS){
    if(is_0000(LminusS)) break;

    int count = INF;
    for(int round=0;round <= 1000000;round++){
      if(is_all_same(LminusS)) break;
      if(is_6174(LminusS)){
	count = round;
	break;
      }
      string smaller = LminusS;
      sort(smaller.begin(),smaller.end());
      
      string larger = LminusS;
      sort(larger.begin(),larger.end(),greater<char>());

      int num = atoi(larger.c_str()) - atoi(smaller.c_str());
      stringstream ss;
      ss << num;
      string next = ss.str();
      while(next.size() < 4) next = "0" + next;
      LminusS = next;
    }

    if(count == INF){
      printf("NA\n");
    }
    else {
      printf("%d\n",count);
    }
  }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<sstream>
#include<string>
#include<iomanip>
using namespace std;
int cnv(string str,bool gFlag){
    sort(str.begin(),str.end());
    if(gFlag)reverse(str.begin(),str.end());
    stringstream ss(str);
    int n;
    ss>>n;
    return n;
}
int main(){
    string str;
    while(cin>>str,str!="0000"){
        bool flag=false;
        for(int i=1;i<4;i++)
            if(str[0]!=str[i])flag=true;

        if(!flag){
            cout<<"NA"<<endl;
            continue;
        }
        int cnt=0;
        while(str!="6174"){
            int n=cnv(str,true)-cnv(str,false);
            stringstream ss;
            ss<<n;
            ss>>str;
            cnt++;
        }
        cout<<cnt<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;




int main(){

	while (true){
		int n, tmp1 = 0, tmp2 = 0, a[4], cnt = 0;
		bool j = true;
		cin >> n;
		if (n == 0)break;
		tmp1 = n;
		while (tmp1 != 6174){
			rep(i, 4){
				a[i] = tmp1 % 10;
				tmp1 /= 10;
			}
			if (a[0] == a[1] && a[1] == a[2] && a[2] == a[3]){
				j = false;
				break;
			}
			sort(a, a + 4);
			tmp1 = a[0] + a[1] * 10 + a[2] * 100 + a[3] * 1000;
			tmp2 = a[3] + a[2] * 10 + a[1] * 100 + a[0] * 1000;
			tmp1 -= tmp2;
			cnt++;
		}
		if (j)cout << cnt << endl;
		else cout << "NA" << endl;
	}

	return 0;
}

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

/* {{{ */
#define PI(N) ((M_PI * (N)) / 180) //?§???????????????¢??????????????????
#define CL(N) (cout << (N) << endl)
#define DCT(N) (cout << "debug " << (N) << endl)
#define REP(i, N) for (int i = 0; i < (N); i++)
/* }}} */

int main()
{
    int n[4];
    string str;
 
    while (cin >> str, str != "0000"){
        REP(i, 4){
            n[i] = str[i] - '0';
        }
 
        if (n[0] == n[1] && n[0] == n[2] && n[0] == n[3]){
            CL("NA");
        }
        else {
            int c = 0;
            while (n[0] != 6 || n[1] != 1 || n[2] != 7 || n[3] != 4){
                 
                char l[4], s[4];
                REP(i, 4){
                    l[i] = s[i] = n[i];
                }
 
                sort(l, l + 4);
                reverse(l, l + 4);
                sort(s, s + 4);
 
                for (int i = 3; 0 <= i; i--){
                    n[i] = l[i] - s[i];
                    if (n[i] < 0){
                        n[i] += 10;
                        l[i - 1]--;
                    }
                }
                c++;
            }
            CL(c);
        }
    }
    return (0);
}

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

int main(){
  char s[5];
  while(scanf("%s",s),strcmp(s,"0000")){
    bool f = true;
    for(int i=0;i<3;i++)f &= (s[i]==s[i+1]);
    if(f){ printf("NA\n"); continue; }

    int cnt = 0;
    while(strcmp(s,"6174")){
      sort(s,s+4);
      int S = atoi(s);
      reverse(s,s+4);
      int L = atoi(s);
      char N[5]; sprintf(N,"%04d",L-S);
      for(int i=0;i<4;i++)s[i] = N[i];
      cnt++;
    }
    printf("%d\n",cnt);
  }
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;

class cl
{
public:
	int s[4];

	void sortw(int a)
	{
		for(int i = 0; i < 4; i++)
		{
			s[i] = a % 10;
			a /= 10;
		}
		sort(s,s + 4);
	}

	int minw()
	{
		int w = 0;
		for(int i = 0; i < 4; i++)
		{
			w *= 10;
			w += s[i];
		}
		return w;
	}

	int maxw()
	{
		int w = 0;
		for(int i = 0; i < 4; i++)
		{
			w *= 10;
			w += s[3 - i];
		}
		return w;
	}
};

int func(int a)
{
	cl w;
	w.sortw(a);
	return w.maxw() - w.minw();
}

int stoi(char* a)
{
	int w = 0;
	for(int i = 0; i < 4; i++)
	{
		w *= 10;
		w += a[i] - '0';
	}
	if(w % 1111 == 0 && w != 0)
	{
		return -1;
	}
	return w;
}

int kai(int a)
{
	for(int i = 0; i < 10000; i++)
	{
		if(a == 6174)
		{
			return i;
		}
		a = func(a);
	}
}

int main()
{
	while(1)
	{
		char w[10];
		scanf("%s",w);
		int a = stoi(w);
		if(a == 0)
		{
			break;
		}
		if(a < 0)
		{
			printf("NA\n");
		}
		else
		{
			printf("%d\n",kai(a));
		}
	}
	return 0;
}