Go: package main

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

var memo []int

func solve(v int) int {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		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([]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<algorithm>
using namespace std;
int main(){
	int N;
	while(1){
		cin>>N;
		if(N==0000)break;
		if(N%1111==0){
			cout<<"NA"<<endl;
		}
		   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;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { -1,0,1,0 }, dy[4] = { 0,-1,0,1 };
const char dir[4] = { 'u','l','d','r' };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        (wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)
# pragma warning(disable:4996)


string s;
vector<int> v(4);

string calc(string s) {
	REP(i, 4)v[i] = s[i] - '0';
	sort(ALL(v));
	int hi = 0, lo = 0;
	REP(i, 4) {
		lo += v[i];
		lo *= 10;
	}
	lo /= 10;
	reverse(ALL(v));
	REP(i, 4) {
		hi += v[i];
		hi *= 10;
	}
	hi /= 10;
	string ret = to_string(hi - lo);
	while (ret.size() != 4)ret = "0" + ret;
	return ret;
}

int main() {
	while (cin >> s&&s != "0000") {
		for (int i = 1;; i++) {
			if (i == 1000) {
				cout << "NA" << endl;
				break;
			}
			if (s == "6174") {
				cout << i-1 << endl;
				break;
			}
			s = calc(s);
		}
	}
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int stoi(string s){
	int size=s.size(),res=0;
	for(int i=size-1;i>=0;i--){
		res+=(int)(s[i]-'0')*pow(10,size-1-i);
	}
	return res;
}

string itos(int n){
	int size=log(n)/log(10)+1;
	string s="";
	rep(i,size){
		s+=(char)(n/pow(10,size-1-i)+'0');
		n%=(int)pow(10,size-1-i);
	}
	return s;
}

int main(){
	string s,res[2];
	int ma,mi;
	while(cin>>s){
		if(s[0]=='0' && s[1]=='0' && s[2]=='0' && s[3]=='0')break;
		if(s[0]==s[1] && s[1]==s[2] && s[2]==s[3]){
			cout<<"NA"<<endl;
			continue;
		}
		int cnt=0;
		while(s!="6174"){
			cnt++;
			sort(s.begin(),s.end());
			mi=stoi(s);
			reverse(s.begin(),s.end());
			ma=stoi(s);
			s=itos(ma-mi);
		}
		cout<<cnt<<endl;
	}
	return 0;
}

C++: #include<stdio.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
#include<string.h>
#include<map>
#include<queue>

using namespace std;

typedef pair<int,int> pii;

#define rep(i,j) for(int i=0;i<(j);i++)
#define reps(i,j,k) for(int i=j;i<=k;i++)
#define in(i,j,k) ((i)>=(j)&&(i)<=(k))

int main(){
	char n[5],l[5],h[5],*k;
	int cnt,t;
	k = "6174";
	while(~scanf("%s",n)){
		cnt=0;
		strcpy(l,n);
		strcpy(h,n);
		sort(l,l+4);
		sort(h,h+4,greater<char>());
		if(*h=='0')break;
		if(!strcmp(h,l)){
			puts("NA");
			continue;
		}
		while(strcmp(n,k)){
			strcpy(l,n);
			strcpy(h,n);
			sort(l,l+4);
			sort(h,h+4,greater<char>());
			rep(i,4){
				n[i]=h[i]-l[i]+'0';
				if(n[i]<'0'){
					n[i]+=10;
					n[i-1]--;
				}
			}
			cnt++;
		}
		printf("%d\n",cnt);
	}
	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;
  }
  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);
    }
  }
}