Go: package main

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

func main() {
	var v int
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		count := 0
		for v != 6174 {
			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))
			v = l - s
			count++
		}
		fmt.Println(count)
	}
}

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

template <class F, class T>
void convert(const F &f, T &t){
	stringstream ss;
	ss << f;
	ss >> t;
}

int main(){
	int x, a, b;
	string s;
	while(scanf("%d", &x), x){
		if(x % 1111 == 0){
			puts("NA");
		}
		else{
			int ans;
			for(ans = 0; x != 6174; ++ans){
				convert(x, s);
				s.resize(4, '0');
				sort(s.begin(), s.end());
				convert(s, a);
				reverse(s.begin(), s.end());
				convert(s, b);
				x = b - a;
			}
			printf("%d\n", ans);
		}
	}
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<cstdlib>

int main() {
	int n;
	int str[4];
	while (std::cin >> n) {
		int str[4] = { (n / 1000),(n / 100) % 10,(n / 10) % 10,n % 10 };
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == 0)
				break;
			else
				std::cout << "NA" << std::endl;
		}
		int cnt = 0;
		while(n!=6174) {
			if (str[0] == str[1] == str[2] == str[3])
				std::cout << "NA" << std::endl;
			++cnt;
			std::sort(str, str + 4, std::greater<int>());
			int l = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
			std::sort(str, str + 4);
			int	s = str[0] * 1000 + str[1] * 100 + str[2] * 10 + str[3];
			n = l - s;
			str[0] = n / 1000;
			str[1] = (n / 100) % 10;
			str[2] = (n / 10) % 10;
			str[3] = n % 10;
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
	
int main(){
	while(1){
		int N;
		int cnt=0;
		cin >>N;

		if(N==0000)
			break;

		if(N%1111==0){
			cout << "NA" <<endl;
			continue;
		}

		while(N!=6174){
			int n[4]={N/1000,N/100%10,N/10%10,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<cstdio>
#include<cstring>
#include<functional>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
int main(void)
{
	int i,j;
	int n,cnt,l,s,flg;
	int a[3];
	while(1) {
		cin >> n;
		if(n==0000) break;
		cnt=0;
		flg=0;
		while(1) {
			if(n==6174) break;
			a[0]=n/1000;
			n=n%1000;
			a[1]=n/100;
			n=n%100;
			a[2]=n/10;
			a[3]=n%10;
			
			if(a[0]==a[1] && a[0]==a[2] && a[0]==a[3]) {cout << "NA" << endl;flg=1;break;}
			sort(a,a+4);
	//		cout << a[0] << a[1] << a[2] << a[3] << endl;
			s=(a[0]*1000)+(a[1]*100)+(a[2]*10)+a[3];
			sort(a,a+4,greater<int>());
			l=(a[0]*1000)+(a[1]*100)+(a[2]*10)+a[3];
	//		cout << a[0] << a[1] << a[2] << a[3] << "==" << l << endl;
			if(l-s==6174) {cnt++;break;}
			else {n=l-s;cnt++;}
		}
		if(flg==0)cout << cnt << endl;
	}
	exit(0);
}

C++: // 2013/06/14 Tazoe

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	while(true){
		string N;
		cin >> N;

		if(N=="0000")
			break;

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

		int cnt = 0;

		int n = (N[0]-'0')*1000+(N[1]-'0')*100+(N[2]-'0')*10+(N[3]-'0');

		while(true){
			if(n==6174){
				cout << cnt << endl;
				break;
			}

			int tmp[4];
			tmp[0] = n/1000;
			tmp[1] = n/100%10;
			tmp[2] = n/10%10;
			tmp[3] = n%10;

			sort(tmp, tmp+4);
			int L = tmp[3]*1000+tmp[2]*100+tmp[1]*10+tmp[0];
			int S = tmp[0]*1000+tmp[1]*100+tmp[2]*10+tmp[3];

			n = L-S;
			cnt++;
		}
	}

	return 0;
}

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

int func(string n){
    if(n[0]==n[1]&&n[1]==n[2]&&n[2]==n[3])return -1;
    
    string L,S;
    
    for (int cnt=0; ; cnt++) {
        if(n=="6174")return cnt;
        L=S=n;
        sort(L.begin(), L.end(), greater<char>());
        sort(S.begin(), S.end());
        int x=atoi(L.c_str())-atoi(S.c_str());
        for (int i=0,p=1000; i<4; i++,p/=10) {
            n[i]='0'+x/p;
            x%=p;
        }
    }
}

int main() {
    while (1) {
        string N;
        cin>>N;
        if(N=="0000")break;
        int ans=func(N);
        if(ans==-1){
            cout<<"NA"<<endl;
        }else{
            cout<<ans<<endl;
        }
    }
}

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

int main(void){
  int N;
  vector<int> nums;
  
  while(cin >> N){
    int times=0;
    if(N==0){
      break;
    }
    if(N%1111==0){
      cout << "NA" << endl;
    }
    else{
      while(N!=6174){
	for(int i=1;i<=4;i++)
	  nums.push_back( (N% (int)pow(10,i) )/(int)pow(10,i-1) );
	N=0;
	sort(nums.begin(),nums.end(),greater<int>());
	for(int i=0;i<4;i++)
	  N+=nums[i]*pow(10,4-i-1)-nums[i]*pow(10,i);
	times++;
	nums.clear();
      } 
      cout << times << endl;
    }
  }
  return 0;
}

C++: #include <stdio.h> // C++???????????????
#include <string.h> // c++???????????????
#include <math.h> // c++???????????????
#include <iostream>
#include <string>
#include <stack>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

typedef long long int lli;
#define PI 3.1415926535897932
#define MAX(a,b) ((a>b)?(a):(b)) // ?????§????????????
#define MIN(a,b) ((a<b)?(a):(b)) // ????°?????????????
#define INRANGE(a,b,c) ((a<=b&&b<=c)?(1):(0)) // a<=b<=c??????????????????????????????
#define N 4

void swap(int &a,int &b){ // c??????*
	int w=a;
	a=b;
	b=w;
}

int getdigit(int num){ // ??´??°????????°?????????
	int i,j;
	if(num<0) num*=-1; // ?????°???????????¢
	if(num==0)return 1;
	for(i=10,j=1;;i*=10,j++){
		if(INRANGE(i/10,num,i-1))return j;
	}
	return 0; // ????????¨??????
}

int getN(int num,int digit){ // ?????????????????°????????????
	int i=1,j;
	for(j=1;j<digit;i*=10,j++);
	return (num/i)%10;
}

int zeller(int y,int m,int d){ // ?????§??????
	if(m<3){y--; m+=12;}
	return (y+y/4-y/100+y/400+(13*m+8)/5+d)%7;
	// 0=sunday!
}

int gcd(int a,int b){	// ?????§??¬?´???°
	if(b==0) return a;
	return gcd(b,a%b);
}

int lcm(int a,int b){	// ????°???¬?????°
	return a*b/gcd(a,b);
}

int stoi(const string str){ // ?????????????????´??°
	int i=0,j;
	int len=str.length();
	int w=0;
	for(i=1,j=1;i<=len && INRANGE('0',str[len-i],'9');i++,j*=10){
		w+=(str[len-i]-'0')*j;
	}
	if(str[0]=='-')w*=-1; // ??????????????????
	return w;
}

string itos(int a){ // ??´??°???????????????
	int i;
	string str="";
	int keta;
	if(a<0){str+='-';a*=-1;} // ?????°??????????????????
	for(keta=10;a/keta!=0;keta*=10);
	for(i=keta/10;i!=0;i/=10){
		str+=((a/i)%10)+'0';
	}
	return str;
}

int main(void){
	string str;
	string str1,str2;
	int a,b; //a>b
	int c;
	while(cin>>str,str!="0000"){
		c=0;
		while(str!="6174"){
			for(int i=0;i<N;i++){
				for(int j=i+1;j<N;j++){
					if(str[i]<str[j])swap(str[i],str[j]);
				}
			}
			str1=str;
			for(int i=0;i<N/2;i++) swap(str[i],str[N-i-1]);
			str2=str;
			str=itos(stoi(str1)-stoi(str2));
			switch(getdigit(stoi(str))){
				case 1:
					str="000"+str;
					break;
				case 2:
					str="00"+str;
					break;
				case 3:
					str="0"+str;
					break;
			}
			if(str=="0000"){cout<<"NA"<<endl; goto LOOP;}
			c++;
		}
		cout<<c<<endl;
LOOP:;
	}
	return 0;
}