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 <bits/stdc++.h>
using namespace std;

int main() {
	string N;
	
	while (cin >> N) {
		if (N == "0000") break;
		int ok = 1, ans = 0;
		unordered_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.find(N) != strs.end()) ok = 0;
		}
		
		if (ok) cout << ans << endl;
		else cout << "NA" << endl;
	}
}

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

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int main(){
    while(true){
	string s;
	ll ans = 0;
	cin >> s;
	if(s == "0000") break;
	else if(s[0] == s[1] && s[0] == s[2] && s[0] == s[3]){
	    cout << "NA" << endl;
	    continue;
	}
	while(s.compare("6174")){
	    vector<int> a(4);
	    vector<int> b(4);
	    a[0] = (int)(s[0] - '0');
	    a[1] = (int)(s[1] - '0');
	    a[2] = (int)(s[2] - '0');
	    a[3] = (int)(s[3] - '0');
	    b[0] = (int)(s[0] - '0');
	    b[1] = (int)(s[1] - '0');
	    b[2] = (int)(s[2] - '0');
	    b[3] = (int)(s[3] - '0');
	    sort(RALL(a));
	    sort(ALL(b));
	    int an = 1000 * a[0] + 100 * a[1] + 10 * a[2] + a[3];
	    int bn = 1000 * b[0] + 100 * b[1] + 10 * b[2] + b[3];
	    int dif = an - bn;
	    if(dif / 1000 >= 1)
		s = to_string(dif);
	    else if(dif / 100 >= 1)
		s = "0" + to_string(dif);
	    else if(dif / 10 >= 1)
		s = "00" + to_string(dif);
	    else s = "000" + to_string(dif);
	    ans++;
	}
	cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <numeric>
#include <algorithm>

using namespace std;

int main(int argc, char **argv) {
  while (true) {
    char s[4];
    cin >> s;
    if (strcmp(s, "0000") == 0) {
      break;
    } else if (s[0] == s[1] && s[0] == s[2] &&
               s[0] == s[3]) {
      cout << "NA" << endl;
      continue;
    } else {
      int n = atoi(s), count = 0, a, b;

      while (true) {
        if (n == 6174)
          break;
        sort(s, s + 4,  greater<char>());
        //cout << s << endl;                                                                                                                                              
        a = atoi(s);
        //cout << a << endl;                                                                                                                                              
        sort(s, s + 4);
        //cout << s << endl;                                                                                                                                              
        b = atoi(s);
        //cout << b << endl;                                                                                                                                              
        n = a - b;
        count++;
        stringstream ss;
        ss << n;
        strncpy(s, ss.str().c_str(), 4);
      }
      cout << count << endl;
    }
  }

  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
	int n,N;
	while((cin>>n)){
		if(n==0)return 0;
		int l2[4],s2[4],c=0;
		while(n!=6174){
			int L=0,S=0;
			l2[0]=n/1000;
			n%=1000;
			l2[1]=n/100;
			n%=100;
			l2[2]=n/10;
			l2[3]=n%10;
			sort(l2,l2+4);
			s2[0]=l2[3];
			s2[1]=l2[2];
			s2[2]=l2[1];
			s2[3]=l2[0];
			int j=0;
			if(l2[0]==l2[1]&&l2[1]==l2[2]&&l2[2]==l2[3]){
			cout<<"NA"<<endl;
			goto masatoy;
			}
			for(int i=1000;i>=1;i/=10){
				L+=l2[j]*i;
				S+=s2[j]*i;
				j++;
			}
//cout<<c<<"?????????"<<n<<endl;
			n=S-L;
			c++;
		}
		cout<<c<<endl;
masatoy:;
	}
}

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

int main()
{
  while (true) {
    string n;
    bool f = false;
    int cnt = 0;
    cin >> n;

    if (n == "0000") {
      break;
    }
    for (int i = 1; i < 4; i++) {
      if (n[0] != n[i]) f = true;
    }
    if (!f) {
      cout << "NA" << endl;
      continue;
    };

    while (n != "6174") {
      vector<char> a, b;
      for (int i = 0; i < 4; i++) {
        a.push_back(n[i]);
      }
      b = a;

      sort(a.begin(), a.end());
      sort(b.begin(), b.end(), greater<char>());

      string hayami = "";
      string kanade = "";
      for (int i = 0; i < 4; i++) {
        hayami += a[i];
        kanade += b[i];
      }

      int tmp_a, tmp_b;
      sscanf(hayami.c_str(), "%d", &tmp_a);
      sscanf(kanade.c_str(), "%d", &tmp_b);

      n = to_string(tmp_b - tmp_a);

      cnt++;
    }
    
    cout << cnt << endl;
  }
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
  char n[5],a[5],b[5];
  int sum,i;
  while(scanf("%s",n),strcmp(n,"0000")!=0){
    
    if(n[0]==n[1]&&n[1]==n[2]&&n[2]==n[3]){
      puts("NA");
      continue;
    }
    
    for(i=0;i<1000;i++){
      
      if(strcmp(n,"6174")==0){
	printf("%d\n",i);
	break;
      }
      
      strcpy(a,n);
      sort(a,a+4);
      reverse(a,a+4);
      strcpy(b,n);
      sort(b,b+4);
      sum = atoi(a)-atoi(b);
      
      n[0] = sum/1000 + '0';
      n[1] = sum/100%10 + '0';
      n[2] = sum/10%10 + '0';
      n[3] = sum%10 + '0';
    }
    
  }
  return(0);
}

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

string i2s(int num){
  stringstream ss;
  string str;
  ss << num;
  ss >> str;
  return str;
}
int main(){
  char num[4];

  while(1){
    for(int i = 0 ; i < 4 ; i++)cin >> num[i];
    if(num[0] == '0' && num[1] == '0' && num[2] == '0' && num[3] == '0')break;
    bool f = false;
    for(int i = 0 ; i < 3 ; i++){
      if(num[i] != num[i+1])f = true;
    }

    if(!f){
      cout << "NA" << endl;
      continue;
    } 

    int cnt = 0,t = 0;
    if(strcmp(num,"6174") == 1)t = 1;
    while(strcmp(num,"6174") != t ){
      cnt++; 
      sort(num,num + 4);
      int s = atoi(num);
      sort(num,num + 4,greater<int>());
      int l = atoi(num);
      strcpy(num,i2s(l-s).c_str());
    }
    cout << cnt << endl;
  }
  return 0;
}