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

int main(){
	int n;
	while(scanf("%d", &n), n){
		int cnt = 0;
		if(n == 6174){
			printf("0\n");
			continue;
		}
		if(n%1111 == 0){
			printf("NA\n");
			continue;
		}
		while(true){
			cnt++;
			int a[4];
			int l = 0, s = 0;
			for(int i = 0; i < 4; i++) a[i] = (n%(int)pow(10,4-i))/(int)pow(10,3-i);
			sort(a,a+4);
			for(int i = 0; i < 4; i++){
				s += a[3-i]*(int)pow(10,i);
				l += a[i]*(int)pow(10,i);
			}
			n = l-s;
			if(n == 6174){
				printf("%d\n", cnt);
				break;
			}
		}
	}
}

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

int main(){
	char input[4];
	char array[4];
	int n, l, s;
	int count;

	while(cin >> input, atoi(input)){
		count = 0;
		n = atoi(input);
		if(atoi(input) % 1111 == 0){
			cout << "NA" << endl;
		}
		else {
			while(n != 6174){
				sprintf(input, "%d", n);
				sort(input, input+4);
				s = atoi(input);
				sort(input, input+4, greater<int>());
				l = atoi(input);
				n = l-s;
				count++;
			}
			cout << count << endl;
		}
	}
}

C++: #include <iostream>
#include <algorithm>

int main() {
	int n;
	int a[4] = {};

	while (std::cin >> n) {
		if (n % 1111 == 0) {
			std::cout << "NA" << std::endl;
			continue;
		}

		int count = 0;
		while (n != 6174) {
			a[0] = n / 1000, n %= 1000;
			a[1] = n / 100, n %= 100;
			a[2] = n / 10, n %= 10;
			a[3] = n;

			std::sort(a, a + 4);

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

			++count;
		}

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

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();
			
			while (N.size() < 4) N = "0" + N;
			
			++ans;
			
			if (strs.count(N)) ok = 0;
		}
		
		if (ok) cout << ans << endl;
		else cout << "NA" << endl;
	}
}

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

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  string n;
  while (cin >> n, n != "0000") {
    int ans = 0;
    bool na = false;
    while (n != "6174") {
      sort(n.begin(), n.end());
      int s = stoi(n);
      sort(n.begin(), n.end(), greater<char>());
      int L = stoi(n);
      if (s == L) {
        cout << "NA\n";
        na = true;
        break;
      }
      ans++;
      ostringstream os;
      os << setfill('0') << setw(4) << L - s;
      n = os.str();
    }
    if (!na) cout << ans << "\n";
  }
}

C++: #include<stdio.h>
int main(void)
{
	int n;
	int l,s;
	int i,j,temp;
	int keta[4];
	int cnt;
	
	scanf("%d",&n);
	
	while(n!=0000){
		if(n%1111==0){
			printf("NA\n");
		}
		while(n!=6174){
			for(i=0;i<4;i++){
				keta[i]=n%10;
				n=n/10;
			}
			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]<keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			l=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];

			for(i=0;i<3;i++){
				for(j=i+1;j<4;j++){
					if(keta[i]>keta[j]){
						temp=keta[i];
						keta[i]=keta[j];
						keta[j]=temp;
					}
				}
			}
			s=keta[0]*1000+keta[1]*100+keta[2]*10+keta[3];
			n=l-s;
			cnt++;
		}
	printf("%d\n",cnt);
	scanf("%d",&n);
	}

	return 0;
}

C++: #include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int f(char* s){
  return (s[0]-'0')*1000+(s[1]-'0')*100+(s[2]-'0')*10+s[3]-'0';
}
int main(){
  int i;
  for(;;){
    char s[5];
    cin>>s;
    if(s[0]=='0'&&s[1]=='0'&&s[2]=='0'&&s[3]=='0')break;
    if(s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3]){
      cout<<"NA"<<endl;
      continue;
    }
    for(i=0;f(s)!=6174;++i){
      char t[5],u[5];
      memcpy(t,s,sizeof(s));
      sort(t,t+4);
      memcpy(u,s,sizeof(s));
      sort(u,u+4,greater<char>());

      int p=f(u)-f(t);
      s[0]=p/1000+'0';
      s[1]=p/100%10+'0';
      s[2]=p/10%10+'0';
      s[3]=p%10+'0';
    }
    cout<<i<<endl;
  }
  return 0;
}

C++: // 2013/07/07 Tazoe

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

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

		if(N==0)
			break;

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

		int cnt = 0;

		while(N!=6174){
			int n[4];
			n[0] = N/1000;
			n[1] = N/100%10;
			n[2] = N/10%10;
			n[3] = 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<iostream>
#include<cmath>
#include<algorithm>
#include<string>
using namespace std;

int N,a[4],S,L,cnt;

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

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

int main(){

  bool d;
  string s;

  while(true){
    cin >> s;
    if(s == "0000") break;

    int ans = 0;
    while(s != "6174"){
      string a = s,b;

      d = true;
      for(int i=0;i<3;i++){
	if(s[i] != s[i+1]) d = false;
      }

      if(d) break;

      sort(a.begin(),a.end());
      b = a;
      reverse(b.begin(),b.end());

      int n = 0,m = 0;
      for(int i=0;i<4;i++){
	n *= 10;
	m *= 10;
	n += a[i] - '0';
	m += b[i] - '0';
      }

      m -= n;

      int k = 1000;
      s = "";

      for(int i=0;i<4;i++){
	s += (m/k + '0');
	m -= m/k * k;
	k /= 10;
      }

      ans++;
    }

    if(d) cout << "NA" << endl;
    else cout << ans << endl;

  }
}

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

typedef long long ll;

ll gcd( ll x, ll y );
ll lcm( ll x, ll y );

int main()
{
    int n;

    while ( true ) {
        scanf("%d", &n );
        if ( n == 0 ) break;
        if ( n % 1111 == 0 ) {
            printf("NA\n");
        } else if ( 6174 % n == 0 ) {
            printf("%d\n", 6174 / n - 1 );
        } else {
            printf("%d\n", 6174 / n );
        }
    }

    return 0;
}

ll gcd( ll x, ll y )
{
    if ( y == 0 ) return x;
    return gcd( y, x % y );
}

ll lcm( ll x, ll y )
{
    return x / gcd( x, y ) * y;
}