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<stdio.h>
int main(void)
{
	int N,L,S,N1,N2,N3,N4,N5,N6,i,temp,j,cnt,flg,flg1;
	int a[4];
	cnt=1;
	flg=0;
	flg1=0;
	scanf("%d",&N);
	if(N==6174){
		flg1=1;
	}
	while(!(N==0000)){
		if(flg1==1){
			cnt=0;
			flg=1;
			break;
		}
		else{
			N1=N/1000;/*6!*/
			N2=N%1000;/*174*/
			N3=N2/100;/*1!*/
			N4=N2%100;/*74*/
			N5=N4/10;/*7*/
			N6=N4%10;/*4!*/
			if(N1==N3 && N1==N5 && N1==N6){
				printf("NA\n");
				break;
			}
			else{
				a[0]=N1;
				a[1]=N3;
				a[2]=N5;
				a[3]=N6;
				for(i=0;i<4;i++){
					for(j=i+1;j<4;j++){
						if(a[i]<a[j]){
							temp=a[j];
							a[j]=a[i];
							a[i]=temp;	
						}
					}
				}
				N1=a[0]*1000;
				N3=a[1]*100;
				N5=a[2]*10;
				N6=a[3];
				L=N1+N3+N5+N6;
				for(i=0;i<4;i++){
					for(j=i+1;j<4;j++){
						if(a[i]>a[j]){
							temp=a[i];
							a[i]=a[j];
							a[j]=temp;	
						}
						
					}
				}
				N1=a[0]*1000;
				N3=a[1]*100;
				N5=a[2]*10;
				N6=a[3];
				S=N1+N3+N5+N6;
				N=L-S;
				if(N==6174){
				
					break;
				}
				else{
				flg=1;
					cnt++;
				}
			}
		}
	}
	if(flg==1){
	printf("%d\n",cnt);
	}
	return 0;
}

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

int main(){
  int n;
  int num[5];
  int l[5],s[5];
  int ans;

  while(1){
    cin >> n;
    if(n==0){
      break;
    }
    for(int i=3;i>=0;i--){
      num[i]=n%10;
      n/=10;
    }
    ans=0;
    while(1){
      if(num[0]==6 && num[1]==1 && num[2]==7 && num[3]==4){
	cout << ans << '\n';
	break;
      }
      if(num[0]==num[1] && num[1]==num[2] && num[2]==num[3]){
	cout << "NA\n";
	break;
      } 
      sort(num,num+4,greater<int>());
      for(int i=0;i<4;i++){
	l[i]=num[i];
      }
      int j=3;
      for(int i=0;i<4;i++){
	s[i]=num[j];
	j--;
      }
      
      for(int i=3;i>=0;i--){
	if(l[i]<s[i]){
	  l[i]+=10;
	  l[i-1]--;
	  num[i]=l[i]-s[i];
	}else{
	  num[i]=l[i]-s[i];
	}
      }
      ans++;
    }
  }
  
  return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    char s[8];
    while (atoi(fgets(s,8,stdin))) {
        *strchr(s,'\n')=0;
        int res=0;
        while (strcmp(s,"6174")) {
            sort(s, s+4);
            int S=atoi(s);
            reverse(s, s+4);
            int L=atoi(s);
            if (S==L) { res=-1; break; }
            res++;
            sprintf(s,"%04d",L-S);
        }
        if (res<0) puts("NA");
        else printf("%d\n",res);
    }
    return 0;
}

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

int main(){
	int num[4],l,s,aa=0,n;
	while(cin>>n,n){
		if(n>999){
			num[0]=n/1000;
			num[1]=n/100%10;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n>99&&n<1000){
			num[0]=0;
			num[1]=n/100%10;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n>9&&n<100){
			num[0]=0;
			num[1]=0;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		if(n<10){
			num[0]=0;
			num[1]=0;
			num[2]=n/10%10;
			num[3]=n%10;
		}
		aa=0;
		if(num[0]==num[1]&&num[1]==num[2]&&num[2]==num[3]){
			cout<<"NA"<<endl;
			break;
		}
		while(num[0]!=6&&num[1]!=1&&num[2]!=7&&num[3]!=4){
			l=0,s=0;
			sort(num,num+4);
			for(int i=0;i<4;i++)cout<<num[i];
			cout<<endl;
			s=num[0]*1000+num[1]*100+num[2]*10+num[3];
			l=num[3]*1000+num[2]*100+num[1]*10+num[0];
			num[0]=(l-s)/1000;
			num[1]=(l-s)/100%10;
			num[2]=(l-s)/10%10;
			num[3]=(l-s)%10;
			aa++;
		}
		cout<<aa<<endl;
	}
}

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

int main() {
	string N;
	
	while (cin >> N) {
		if (N == "0000") break;
		int ok = 1, ans = 0;
		set< string > strs;
		
		while (ok) {
			if (N == "6174") break;
			strs.insert(N);
			
			string L = N, S = N;
			sort(L.begin(), L.end(), greater< char >());
			sort(S.begin(), S.end());
			
			stringstream ss;
			ss << L << " " << S;
			int l, s;
			ss >> l >> s;
			
			int n = l - s;
			stringstream sss;
			sss << n;
			N = sss.str();
			
			if (strs.count(N)) ok = 0;
			
			++ans;
		}
		
		if (ok) cout << ans << endl;
		else cout << "NA" << endl;
	}
}

C++: #include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;



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

	int N = 1;
	while (N) {
		string S; cin >> S;
		N = (S[0] - '0') * 1000 + (S[1] - '0') * 100 + (S[2] - '0') * 10 + (S[3] - '0');
		if (!N)break;
		VI A(4), B(4);
		A[0] = B[0] = N % 10;
		A[1] = B[1] = (N / 10) % 10;
		A[2] = B[2] = (N / 100) % 10;
		A[3] = B[3] = N / 1000;
		SORT(A);
		rSORT(B);
		if (A == B) {
			cout << "NA" << '\n';
			continue;
		}
		int ans = 0;
		while (N != 6174) {
			ans++;
			int b = B[0] * 1000 + B[1] * 100 + B[2] * 10 + B[3];
			int a = A[0] * 1000 + A[1] * 100 + A[2] * 10 + A[3];
			N = b - a;
			A[0] = B[0] = N % 10;
			A[1] = B[1] = (N / 10) % 10;
			A[2] = B[2] = (N / 100) % 10;
			A[3] = B[3] = N / 1000;
			SORT(A);
			rSORT(B);
		}
		cout << ans << endl;
	}

	return 0;
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

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

int main(){
	int n;
	int cnt;
	int num[4];
	while(scanf("%d",&n)&&n){
		cnt = 0;
		if(n%1111==0){
			cout <<"NA"<<endl;
			continue;
		}
		while(n!=6174){
			cnt++;
			int tmp =n;
			for(int i =0;i <4;i++){
				num[i]=tmp%10;
				tmp /=10;
			}
			int x = 0;
			sort(num,num+4,greater<int>());
			for(int i =0;i <4;i++){
				x =x*10+num[i];
			}
			int L,S;
			L =x;
			sort(num,num+4);
			x = 0;
			for(int i =0;i <4;i++){
				x =x*10+num[i];
			}
			S =x;
			n =L-S;
		}
		cout <<cnt<<endl;
	}
	return 0;
}