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>
#include<functional>
#include<cstring>
#include<cstdlib>

int main() {
	char str[4];
	while (std::cin >> str) {
		if (str[0] == str[1] == str[2] == str[3]) {
			if (str[0] == '0')
				break;
			else
				std::cout << "NA" << std::endl;
		}
		char l[4], s[4];
		int cnt = 0;
		while(str!="6174") {
			++cnt;
			std::memcpy(l, str, 4);
			std::memcpy(s, str, 4);
			std::sort(l, l + 4, std::greater<char>());
			std::sort(s, s + 4);
			int a = atoi(l) - atoi(s);
			itoa(a, str, 10);
		}
		std::cout << cnt << std::endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>

using namespace std;

int main(){
	int a[4];
	int b,flg,c,d,e,cunt,m,f;
	
	scanf("%d",&d);
	while(d!=0000){
		
		m=1000;
		cunt=0;
		flg=0;
		
		f=d;
		
		for(int i=0;i<3;i++){
			a[3-i]=d/m;
			d=d%m;
			m/=10;
		}
		a[0]=d;
		
		for(int i=0;i<3;i++){
			if(a[i]!=a[i+1]) flg=1;
		}
		
		
		
		if(flg==1){
			while(f!=6174){
				c=0;
				e=0;
				sort(a,a+4);
				m=1;
				for(int i=0;i<4;i++){
					c+=a[i]*m;
					m*=10;
				}
				sort(a,a+4,greater<int>());
				m=1;
				for(int i=0;i<4;i++){
					e+=a[i]*m;
					m*=10;
				}
				
				d=c-e;
				
				
				f=d;
				
				m=1000;
				
				for(int i=0;i<3;i++){
					a[3-i]=d/m;
					d=d%m;
					m/=10;
				}
				a[0]=d;
				cunt++;
			}
		}
		
		if(flg==0){
			printf("NA\n");
		}else{
			printf("%d\n",cunt);
		}
		scanf("%d",&d);
	}

}

C++: #include<stdio.h>
int main(void)
{
	int a,s,d[5],f,g,h,j,i,k;
	scanf("%d",&a);
	while(a!=0000){
		s=a;
		h=0;
		k=1;
		while(s!=6174){
			for(i=1;i<=4;i++){
				d[i]=s%10;
				s=s/10;
			}
			if(d[1]==d[2]&&d[2]==d[3]&&d[3]==d[4]){
				k=0;
				break;
			}
			for(i=1;i<=4;i++){
				for(j=i;j<=4;j++){
					if(d[i]<d[j]){
						g=d[i];
						d[i]=d[j];
						d[j]=g;
					}
				}
			}
			h++;
			s=(d[1]*1000+d[2]*100+d[3]*10+d[4])-(d[4]*1000+d[3]*100+d[2]*10+d[1]);
			//printf("%d\n",s);
		}
		if(k==1){
			printf("%d\n",h);
		}
		if(k==0){
			printf("NA\n");
		}
		scanf("%d",&a);
	}
	return 0;
}

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

int s(int n, bool f) {
  vector<int> a;
  while (n) {
    a.push_back(n % 10);
    n /= 10;
  }

  sort(a.begin(), a.end());
  if (f) reverse(a.begin(), a.end());

  int res = 0;
  for (int i = 0; i < a.size(); ++i) {
    res *= 10;
    res += a[i];
  }

  return res;
}

int main() {
  int n;

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

    // cout << s(n, true) - s(n, false) << endl;

    int cnt = 0;
    while (n != 6174) {
      cnt++;
      n = s(n, true) - s(n, false);
    }

    cout << cnt << endl;
  }
}

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

int stoi(string s){
  stringstream ss(s);
  int res; ss >> res;
  return res;
}

int main(){
  string s;
  while(cin >> s,s!="0000"){
    bool f = true;
    for(int i=0;i<3;i++)f &= (s[i]==s[i+1]);
    if(f){ cout << "NA\n"; continue; }

    int cnt = 0;
    while(s!="6174"){
      sort(s.begin(),s.end());
      int S = stoi(s);
      reverse(s.begin(),s.end());
      int L = stoi(s);
      char N[5]; sprintf(N,"%04d",L-S);
      for(int i=0;i<4;i++)s[i] = N[i];
      cnt++;
    }
    cout << cnt << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int m,n,l;
  cin>>n;
  while(n!=0){
    int nn[4]={n%10,(n/10)%10,(n/100)%10,(n/1000)%10};
    if(nn[0]==nn[1]&&nn[1]==nn[2]&&nn[2]==nn[3]){
      cout<<"NA"<<endl;
    }else{
      int cou=0;
      while(nn[0]!=4&&nn[1]!=7&&nn[2]!=1&&nn[3]!=6){
	int rn[4];
	for(int i=0;i<3;i++){
	  for(int j=i+1;j<4;j++){
	    if(nn[i]>nn[j]){
	      int exi=nn[i];
	      nn[i]=nn[j];
	      nn[j]=exi;
	    }
	  }
	}
	for(int i=0;i<4;i++){
	  rn[i]=nn[i];
	}
	for(int i=0;i<4;i++){
	  if(rn[i]-nn[3-i]<0){
	    rn[i]+=10;
	    rn[i]-=nn[3-i];
	    rn[i+1]--;
	  }else{
	    rn[i]-=nn[3-i];
	  }
	}
	for(int i=0;i<4;i++){
	  nn[i]=rn[i];
	}
	cou++;
      }
      cout<<cou<<endl;
    }
    cin>>n;
  }
  return (0);
}

C++: #include<bits/stdc++.h>
#define LIM 1+2+4+8+16+32+64+128
using namespace std;
typedef long long int ll;
typedef struct{
  int per;
  int ko;
  int kyoda;
  int konoko;
}TRE;
int ace(int a,int b){
  int ss=0;
  for(int i=b;i<=a;i++){
    ss+=i;
  }
  return(ss);
}
typedef pair<int,int>P;
int main(){
  int n;
  while(1){
    cin>>n;
    if(n==0){
      break;
    }
    if(n%1111==0){
      cout<<"NA"<<endl;
    }else{
      int cnt=0;
      while(n!=6174){
	cnt++;
	int l=0;
	int s=0;
	int sl[4];
	int ss=0;
	for(int i=10;i<=1000;i*=10){
	  sl[ss]=(n-((n/i)*i))/(i/10);
	  ss++;
	}
	sl[ss]=n/1000;
	sort(sl,sl+4);
	ss=0;
	for(int i=1;i<=1000;i*=10){
	  l+=sl[4-ss-1]*i;
	  s+=sl[ss]*i;
	  ss++;
	}
	n=abs(l-s);
      }
      cout<<cnt<<endl;
    }
  }
  return(0);
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main(void){
	string S;
	while(cin >> S){
		int cost = 0;
		string str = S;
		if(S == "6174"){
			cout << cost << endl;
			continue;
		}
		if(S == "0000")break;
		if(S[0] == S[1] && S[2] == S[3] && S[0] == S[3]){
			cout << "NA" << endl;
			continue;
		}
		while(1){
			cost++;
			vector<int> v;
			rep(i,4){
				int a = str[i]-'0';
				v.push_back(a);
			}
			sort(v.begin(), v.end(), greater<int>());
			int L = v[0] * 1000 + v[1] * 100 + v[2] * 10 + v[3];
			sort(v.begin(), v.end());
			int S = v[0] * 1000 + v[1] * 100 + v[2] * 10 + v[3];
			int sum = L - S;
			if(sum == 6174){
				cout << cost << endl;
				break;
			}
			str = IntToStr(sum);
		}


	}
	return 0;
}



int StrToInt(string s){
	stringstream ss;
	ss << s;
	int val;
	ss >> val;
	return val;
}
string IntToStr(int i){
	stringstream ss;
	ss << i;
	return ss.str();
}

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");
		return 0;
		}
		cnt=0;
		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;
}