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.Print("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<iostream>
#include <algorithm>
using namespace std;

int main(){
	while(1){
		int n[4];
		int L=0;
		int S=0;
		int N=0;
		cin>>N;
		if(N==0){
			break;
		}
		if(N%1111==0){
			cout<<"NA"<<endl;
			continue;
		}
		int cun=0;
		int B=1;
		while(B){
			if(N==6174){
				B=0;
				break;
			}
			cun++;
			n[0]=N/1000;
			n[1]=N/100%10;
			n[2]=N/10%10;
			n[3]=N%10;
			
			sort(n,n+4);
			
			S=n[0]*1000+n[1]*100+n[2]*10+n[3];
			L=n[3]*1000+n[2]*100+n[1]*10+n[0];

			N=L-S;
			
			
		}
		cout<<cun<<endl;
	}
	return 0;
}

C++: //39
#include<iostream>
#include<cstdlib>
#include<sstream>
#include<string>
#include<algorithm>

using namespace std;

int main(){
  for(string s;cin>>s,s!="0000";){
    int c=0;
    while(s!="6174"){
      sort(s.begin(),s.end());
      if(s[0]==s[3]){
	cout<<"NA"<<endl;
	goto next;
      }
      stringstream ss;
      ss.fill('0');
      ss.width(4);
      ss<<atoi(string(s.rbegin(),s.rend()).c_str())-atoi(s.c_str());
      s=ss.str();
      c++;
    }
    cout<<c<<endl;
  next:
    ;
  }
  return 0;
}

C++: #include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <sstream>
#include <string>
#include <map>
#include <set>
using namespace std;

#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)
#define all(c) (c).begin(), (c).end()

int main(){
    int a;
    while(cin >> a && a){
        if(a%1111==0){
            printf("NA\n");
            continue;
        }
        int ans=0;
        while(a!=6174){
            int d[4];
            rep(i,4){
                d[i]=a%10;
                a/=10;
            }
            sort(d,d+4);
            int b=0;
            rep(i,4){
                b*=10;
                b+=d[i];
            }
            int c=0;
            for(int i=3;i>=0;i--){
                c*=10;
                c+=d[i];
            }
            a=c-b;
            ans++;
        }
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <sstream>
#include <vector>
#include <map>
#include <cassert>
using namespace std;

int main(){
	string s;
	while(cin >> s && s != "0000"){
		if( count(s.begin(),s.end(),s[0]) == 4 ){
			cout << "NA" << endl;
			continue;
		}
		int cnt = 0;
		while( s != "6174" ){
			string l,g;
			l=g=s;
			sort(l.begin(),l.end());
			sort(g.rbegin(),g.rend());
			int a = atoi(l.c_str());
			int b = atoi(g.c_str());
			int r = b - a;
			char c[10];
			sprintf(c,"%04d",r);
			s = string(c);
			cnt++;
		}
		cout << cnt << endl;
	}
}

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

int main(){
	int num[4],l,s,aa=0;
	string n;
	while(cin>>n,n!="0000"){
		for(int i=0;i<4;i++)num[i]=n[i]-'0';
		aa=0;
		if(num[0]==num[1]&&num[1]==num[2]&&num[2]==num[3]){
			cout<<"NA"<<endl;
			continue;
		}
		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++: // 0259
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int my_pow(int x, int y){
	int z = 1;
	for(int i=0;i<y;i++)
		z *= x;
	return z;
}

int main(){
	while(1){
		string str;
		int N = 0, count = 0;
		vector<int> num;

		cin>>str;
		if(str == "0000")
			break;

		num.resize(4);
		for(int i=0;i<4;i++){
			num[i] = str[i]-'0';
			N += num[i]*my_pow(10, 3-i);
		}

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

		while(N != 6174){
			for(int i=0;i<4;i++){
				num[i] = N%10;
				N /= 10;
			}

			sort(num.begin(), num.end());

			int L=0, S=0;
			for(int i=0;i<4;i++){
				S += num[i]*my_pow(10, 3-i);
				L += num[i]*my_pow(10, i);
			}

			N = L-S;
			count++;
		}

		cout<<count<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

bool check(string &s)
{
    return (s[0] == s[1] && s[1] == s[2] && s[2] == s[3]);
}

int main()
{
    string N;
    while (cin >> N, N != "0000") {
        int cnt = 0;
        bool fail = 0;
        while (N != "6174") {
            if (check(N)) {
                fail = 1;
                break;
            }                
            string L = N, S = N;
            sort(L.begin(), L.end(), greater<char>());
            sort(S.begin(), S.end());
            auto diff = stoi(L) - stoi(S);
            N = to_string(diff);
            cnt++;            
        }
        if (fail) {
            cout << "NA" << endl;
        } else {
            cout << cnt << endl;
        }
    }        
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
	string a;
	while (cin >> a&&a != "0000") {
		int b[4], l = 0, s = 0, f = atoi(a.c_str()), sum = 0;
		if (a[0] != a[1] && a[1] != a[2] && a[2] != a[3]) {
			while (f != 6174) {
				a = to_string(f);
				for (int i = 0; i < 4; i++) {
					b[i] = (int)a[i] - '0';
				}
				sort(b, b + 4);
				for (int i = 3; i >= 0; i--) {
					l += (pow(10, i)*b[i]);
				}
				for (int i = 0; i < 4; i++){
					s += (pow(10,3 - i)*b[i]);
				}
				f = l - s;
				sum++;
				l = 0;
				s = 0;
			}
			cout << sum << endl;
		}
		else 
			cout << "NA" << endl;
	}
}

C++: #include "stdio.h"
#include "math.h"

#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

const int INF = 10000000;
using namespace std;
typedef long long  ll;

/** Problem0259 : All Numbers Lead to 6174 **/
int main()
{
	string s;
	int  min, max, num[4], n;
	
	while (cin>>s, s!="0000") {
		n=1;
		if (s=="6174") {
			cout << 0 << endl;
			continue;
		} else if (s[0]==s[1] && s[0]==s[2] && s[0]==s[3]) {
			cout << "NA" << endl;
			continue;
		}
		while (1) {
			min=0; max=0;
			fill(num, num+4, 0);
			rep(i, s.length()) {
				num[i] = s[i]-'0';
			}
			sort(num, num+4);
			rep(i, 4) {
				min*=10;
				min+=num[i];
			}
			reverse(num, num+4);
			rep(i, 4) {
				max*=10;
				max+=num[i];
			}
			int nn = max-min;
			if (nn == 6174) {
				cout << n << endl;
				break;
			} else {
				s="";
				rep(i, 4) {
					s+= nn%10;
					nn/=10;
				}
			}
			n++;
		}
	}
	
	return 0;
}