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 <sstream>
#include <algorithm>
using namespace std;

int main() {
  string s;
  while(cin >> s && s!="0000") {
    int cnt=0;
    bool ck=true;
    while(s!="6174") {
      cnt++;
      int a[4];
      fill(a,a+4,0);
      for(int i=0; i<4; i++) a[i]=(int)s[i]-48;
      sort(a,a+4);
      int b=0,c=0,k1=1,k2=1000;
      for(int i=0; i<4; i++) {
	b+=a[i]*k1;
	c+=a[i]*k2;
	k1*=10;
	k2/=10;
      }
      stringstream ss;
      ss << b-c;
      ss >> s;
      if(s.size()<4) {
	while(s.size()<4) s="0"+s;
      }
      if(s=="0000") {
	cout << "NA" << endl;
	ck=false;
	break;
      }
    }
    if(ck) cout << cnt << endl;
  }
  return 0;
}

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

int main(){
  int t[4];
  int tt[4];
  int a,b,bb,c;
  while(true){
    cin>>a;
    c=0;
    if(a==0)break;
    for(int i=1111;i<10000;i+=1111){
      if(i==a){
	cout<<"NA"<<endl;
	c=-1;
      }
    }
    if(c==-1)continue;
    while(a!=6174){
      b=a;
      c++;
      for(int i=3;i>=0;i--){
	t[i]=b%10;
	b/=10;
      }
      sort(t,t+4);
      reverse(t,t+4);
      b=t[0]*1000+t[1]*100+t[2]*10+t[3];

      bb=a;
      for(int i=3;i>=0;i--){
	t[i]=bb%10;
	bb/=10;
      }
      sort(t,t+4);
      bb=t[0]*1000+t[1]*100+t[2]*10+t[3];
      
      a=b-bb;
      //cout<<a<<endl;
    } 
    cout<<c<<endl;
    
  }
  return 0;
}

C++: /*
 * src.cpp
 *
 *  Created on: 2016/08/08
 *      Author: joi
 */

#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)

char N[5];

char L[5],S[5];
char g[5]="6174";

void itoa(int x){
	REP(i,4){
		N[3-i]=x%10+'0';
		x/=10;
	}
}

bool isend(){
	REP(i,4){
		if (N[i]!='0'){
			return false;
		}
	}
	return true;
}

bool isNA(){
	char c=N[0];
	FOR(i,1,4){
		if (N[i]!=c){
			return false;
		}
	}
	return true;
}

int main(){
	do{
		scanf("%s",N);
		if (!isend()){
			if (isNA()){
				printf("NA\n");
			}else{
				int ans=0;
				while (strcmp(N,g)){
					strcpy(L,N);
					strcpy(S,N);
					sort(L,L+4);
					reverse(L,L+4);
					sort(S,S+4);
					itoa(atoi(L)-atoi(S));
					ans++;
				}
				printf("%d\n",ans);
			}
		}
	}while (!isend());
	return 0;
}

C++: //ソートを使いたくなかっただけなんだ
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;

int main()
{
	while (true)
	{
		string N;
		cin >> N;
		if (N == "0000")
		{
			break;
		}
		else if (N[0]==N[1] && N[1]==N[2] && N[2]==N[3])
		{
			cout << "NA" << endl;
			continue;
		}

		int cnt1 = 0;
		while (N != "6174")
		{
			int cnts[10];
			for (int i=0; i<10; i++)
			{
				cnts[i] = 0;
				unsigned int j = 0;
				while ((j=N.find('0'+i, j)) != string::npos)
				{
					++j;
					++cnts[i];
				}
			}
			string L="0000", S="0000";
			int cnt2 = 0;
			for (int i=0; i<10; i++)
			{
				for (int j=0; j<cnts[i]; j++)
				{
					L[3-cnt2] = S[cnt2] = '0'+i;
					++cnt2;
				}
			}

			stringstream ss_L(L), ss_S(S);
			int i_L, i_S;
			ss_L >> i_L;
			ss_S >> i_S;
			stringstream ss_N;
			ss_N << setfill('0') << setw(4) << i_L-i_S;
			N = ss_N.str();
			++cnt1;
		}
		cout << cnt1 << endl;
	}

	return 0;
}

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

#define ASC(vec) vec.begin(), vec.end()		// 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend()	// 降順ソート 例:sort(DESC(vec));
#define rep(i, n) for(int i = 0; i < n; i++)
#define Rep(i, n) for(int i = 1; i < n; i++)
#define In(vec) rep(i, n) cin>>vec[i];
#define ANS(s) cout << s << endl
#define inc(a, b) cin >> a >> b;

const int mod = 1000000007;
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vii = vector<pair<int, int>>;

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
int round_int(int a, int b) { return (a + (b - 1)) / b; }


// 4,8方向に探索する際に用いる配列
int dx[4] = { 0,  0, 1, -1 };
int dy[4] = { 1, -1, 0,  0 };
//int dx[8] = { 1, 0, -1,  0, 1, -1, -1,  1 };
//int dy[8] = { 0, 1,  0, -1, 1,  1, -1, -1 };

constexpr auto MAX = 100010;

// ────────────────────────────────────────────────────

string solve(string s) {
	sort(DESC(s));
	int ma = atoi(s.c_str());
	sort(ASC(s));
	int mi = atoi(s.c_str());

	ostringstream sout;
	sout << setfill('0') << setw(4) << to_string(ma - mi);
	string ret = sout.str();
	return ret;
}

int main() {
	while (1) {
		string s; cin >> s;
		int tmp = atoi(s.c_str());
		if (!tmp) break;

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

		int cnt = 0;
		while (s != "6174") {
			s = solve(s);
			cnt++;
		}
		cout << cnt << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
// define
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a+mod-b)%mod
#define MUL(a, b) a = (((a)%mod) * ((b)%mod)) % mod
#define rollcall cout << "I'm Sucu." << endl;
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define dmt(x, y, z) make_tuple(x, y, z)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define fi first
#define se second
// typedef
typedef complex<double> Point;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9+7;
const int INF = 1e18+7;
// int sum = accumulate(a, a+n, 0LL);
// int mx = max_element(a, a+n);
// int mn = min_element(a, a+n);
// int cnt = count(a, a+n, k);


string to_str(int num){
  string str;
  drep(i,3,0){
    int c = 1;
    rep(j,i)c *= 10;
    str.pb(num/c + '0');
    num = num%c;
  }
  return str;
}

signed main(){
  while(true){
    string str;cin >> str;
    if(str == "0000")break;
    if(str[0] == str[1] and str[1] == str[2] and str[2] == str[3]){
      cout << "NA" << endl;continue;
    }
    int cnt = 0;
    while(true){
      if(str == "6174")break;
      string s1, s2;
      sort(all(str));
      s1 = str;
      reverse(all(str));
      s2 = str;
      int res = atoll(s2.c_str()) - atoll(s1.c_str());
      str = to_str(res);
      cnt++;
      // cout << s1 << " " << s2 << " " << str << endl;
    }
    cout << cnt << endl;
  }
  return 0;
}