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++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
bool NA(string s) {
	if (s == "1111" || s == "2222" || s == "3333" || s == "4444" || s == "5555" || s == "6666" || s == "7777" || s == "8888" || s == "9999")return true;
	else return false;
}
signed main() {
	string s;
	while (cin >> s, s != "0000") {
		if (NA(s))cout << "NA" << endl;
		else {
			int ans(0);
			while (s != "6174") {
				string big(s), small(s);
				sort(all(small));
				sort(all(big), greater<char>());
				s = to_string(stoi(big) - stoi(small));
				ans++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <sstream>

#define rep(i, j) FOR(i, 0, j)
#define FOR(i, j, k) for(int i = j; i < k; ++i)

using namespace std;

int main(){
  char n[5];
  while(scanf("%s", n) && strcmp(n, "0000") != 0){
    if(n[0] == n[1] && n[1] == n[2] && n[2] == n[3]){ puts("NA"); continue;}
    int c = 0;
    while(strcmp(n, "6174") != 0){
      rep(i, 4) FOR(j, i + 1, 4){
	if(n[i] > n[j]){ char tmp = n[i]; n[i] = n[j]; n[j] = tmp;}
      }
      int a = atoi(n);
      char tmp = n[0]; n[0] = n[3]; n[3] = tmp;
      tmp = n[1]; n[1] = n[2]; n[2] = tmp;
      int b = atoi(n);
      b -=  a;
      sprintf(n, "%d", b);
      c++;
    }
    printf("%d\n", c);
  }
  return 0;
}

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

string toString(int n){
  ostringstream oss;
  oss << n;
  return oss.str();
}

int toInt(string s){
  istringstream iss(s);
  int ret;
  iss >> ret;
  return ret;
}


int main(){
  string s;
  while(cin >> s){
    if(s == "0000") break;
    if(s[0] == s[1] && s[1] == s[2] && s[2] == s[3]) cout << "NA" << endl;
    
    int cnt = 0;
    
    while(s != "6174"){
      cnt++;
      string L, S;
      L = s;
      sort(L.begin(), L.end());
      S = L;
      reverse(L.begin(), L.end());
      int a, b;
      a = toInt(L);
      b = toInt(S);
      s = toString(a-b);
    }
    cout << cnt << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

char s[10];
int main() {
	while (scanf("%s", s), strcmp(s, "0000")) {
		int cnt = 0;
		if (s[0] == s[1] && s[0] == s[2] && s[0] == s[3]) {
			puts("NA");
			continue;
		}
		while (strcmp(s, "6174")) {
			sort(s, s + 4); int a = atoi(s);
			reverse(s, s + 4); int b = atoi(s);
			sprintf(s, "%04d", b - a);
			cnt++;
		}
		printf("%d\n", cnt);
	}
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back

vector<int> solve(vector<int> a,vector<int> b){
    string x,y;
    rep(i,a.size()){
        x+='0'+a[i];
    }
    rep(i,b.size()){
        y+='0'+b[i];
    }
    int r=stoi(x)-stoi(y);
    vector<int> ret;
    stringstream ss;
    ss<<r;
    string z=ss.str();
    string t="";
    rep(i,4-z.size())t+="0";
    z=t+z;
    rep(i,z.size())ret.pb(z[i]-'0');
    return ret;
}

int main(){
    string s;
    while(cin>>s){
        if(s=="0000")break;
        int x=0;
        vector<int> c;
        rep(i,s.size())c.pb(s[i]-'0');
        while(1){
            if(c[0]==6&&c[1]==1&&c[2]==7&&c[3]==4){cout<<x<<endl;break;}
            if(c[0]==c[1]&&c[1]==c[2]&&c[2]==c[3]){
                cout<<"NA"<<endl;
                break;
            }
            vector<int> l=c,s=c;
            sort(all(s));
            sort(all(l),greater<int>());
            c=solve(l,s);
            x++;
        }
    }
}

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, 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(num, num + 4);
      l = s = 0;
      REP(i, 4){
        l = l * 10 + num[3 - i];
        s = s * 10 + num[i];
      }
      n = l - s;
      ct++;
    }
    cout << ct << endl;
  }
}

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

int main()
{
    string str;

    while( cin >> str )
    {
        if( str == "0000" )
            break;
        if( str[0] == str[1] && str[2] == str[3] && str[0] == str[2] )
            cout << "NA" << endl;
        else
        {
            int cnt = 0;
            while( str != "6174" )
            {
                for( int i = 0; i < 4; i++ )
                {
                    for( int j = 3; j >= i + 1; j-- )
                    {
                        if( str[j] < str[j-1] )
                        {
                            char temp = str[j];
                            str[j] = str[j-1];
                            str[j-1] = temp;
                        }
                    }
                }

                int l = 0, r = 0, d = 1;
                for( int i = 0; i < 4; i++ )
                {
                    l += ( str[i] - '0' ) * d;
                    r += ( str[3-i] - '0' ) * d;
                    d *= 10;
                }

                l -= r;
                d = 1000;
                for( int i = 0; i < 4; i++ )
                {
                    str[i] = ( l / d ) % 10 + '0';
                    d /= 10;
                }
                cnt++;
            }
            cout << cnt << endl;
        }
    }
    return 0;
}