Go: package main

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

var memo []int8

func solve(v int) int8 {
	if v == 6174 {
		return 0
	}
	if memo[v] == 0 {
		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))
		memo[v] = solve(l-s) + 1
	}
	return memo[v]
}

func main() {
	var v int
	memo = make([]int8, 10000)
	for {
		fmt.Scan(&v)
		if v == 0 {
			break
		}
		if v%1111 == 0 {
			fmt.Println("NA")
			continue
		}
		fmt.Println(solve(v))
	}
}

C++: #include<iostream>
#include<string>
#include<sstream>
#include<vector>
#include <strstream>
#include<algorithm>

#include<functional>

using namespace std;


string IntToString(int number)
{
stringstream ss;
ss << number;
return ss.str();
}

int main(){
	string s;
	while(cin>>s)
	{
		if(s=="0000")break;
		int count=0;
		while (1)
		{
			
			if(s=="6174"){
				puts("0");
				break;
			}
			
			vector<int>n;
			
			for(int i=0; i<4; i++)
			{
				
				n.push_back( (int)s[i]-48 );
			}
			
			sort( n.begin(),n.end(),greater<int>() );
			int L=0,S=0;

			if( (n[0]+n[1]) - (n[2]+n[3]) == 0){
				puts("NA");
				break; 
			}
			
			L += n[0]*1000;
			L += n[1]*100;
			L += n[2]*10;
			L += n[3];
			
			reverse(n.begin(),n.end());
			
			S += n[0]*1000;
			S += n[1]*100;
			S += n[2]*10;
			S += n[3];
			
			s.clear();
			s = IntToString(L-S);
			count++;
			
			if(L-S==6174){
				cout<<count<<endl;
				break;
			}
		}

	}
	return 0;
}

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

int solve(string &s, int cnt = 0){
  while(s != "6174"){
    if(s == "0000")return -1;
    cnt++;
    sort(s.begin(), s.end());
    string tmp = s;
    reverse(tmp.begin(), tmp.end());
    int a = stoi(s), b = stoi(tmp);
    s = to_string(b - a);
    while(s.length() != 4)s.push_back('0');
  }
  return cnt;
}

int main(int argc, char *argv[]){
  string s;
  while(cin >> s){
    if(s == "0000")break;
    int ans = solve(s);
    if(ans != -1)
      std::cout << ans << std::endl;
    else
      std::cout << "NA" << std::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(StrToInt(S) % 1111 == 0){
			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;
			//cout << "L:" << L << " S:" << S << endl;
			//cout << "cost:" << cost << " " << sum << endl;
			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<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  while(cin>>n,n){
    for(int i=0;i<4000;i++){
      if(n==6174){cout<<i<<endl;break;}
      vector<int>v;
      for(int j=0;j<4;j++){
        v.push_back(n%10);
        n/=10;
      }
      sort(v.begin(),v.end());
      if(v[0]==v[3]){cout<<"NA"<<endl;break;}
      int p1,p2;
      p1=v[0]+v[1]*10+v[2]*100+v[3]*1000;
      p2=v[3]+v[2]*10+v[1]*100+v[0]*1000;
      n=p1-p2;
    }
  }
}

C++: #include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main(){
  while(true){
    string s;cin>>s;
    if(s=="0000"){
      return 0;
    }
    int k = 0;
    while(true){
      if(s=="6174"){
        cout << k << endl;
        break;
      }
      k++;
      sort(s.begin(),s.end());
      //cout <<"!!" <<s << endl;
      int a = 0;
      for(int i = 0; 4 > i; i++){
        a*=10;
        a+=s[i]-'0';
        //cout << a << endl;
      }
      int b = 0;
      for(int i = 3; 0 <= i; i--){
        b*=10;
        b+=s[i]-'0';
      }
      //cout << a << " " << b << endl;
      int c = b-a;
      //cout <<c << endl;
      if(c == 0){
        cout << "NA" << endl;
        break;
      }
      if(c == 6174){
        cout << k << endl;
        break;
      }
      s[0] = c/1000;
      s[1] = (c/100)%10;
      s[2] = (c/10)%10;
      s[3] = c%10;
    }
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  while(1){
    int cnt=0;
    string s,l,n;
    cin>>n;
    if(n=="0000")break;
    if(n[0]==n[1]&&n[1]==n[2]&&n[2]==n[3]){
      cout<<"NA"<<endl;
      continue;
    }
    while(1){
      if(n=="6174")break;
      s=n;
      l=n;
      sort(s.begin(),s.end());
      l=s;
      reverse(l.begin(),l.end());
      int t=1,ns=0,nl=0;
      for(int i=3;i>=0;i--){
	ns+=(s[i]-'0')*t;
	t*=10;
      }
      t=1;
      for(int i=3;i>=0;i--){
	nl+=(l[i]-'0')*t;
	t*=10;
      }
      n="";
      int temp=nl-ns;
      for(int i=0;i<4;i++){
	int m=temp%10;
	n+=((char)m+'0');
	temp/=10;
      }
      reverse(n.begin(),n.end());
      cnt++;
    }
    cout<<cnt<<endl;
  }
  return(0);
}

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

string cn;
int main()
{
  while(true){
    cin >> cn;
    if(cn == "0000"){
      break;
    }
    if(cn[0] == cn[1] == cn[2] == cn[3]){
      cout << "NA" << endl;
    }

    else{
      int ans = 0;
      while(cn != "6174"){
	int in, intss;
	stringstream ss1, ss2, ss3;
	string cncp;
	sort(cn.begin(), cn.end(), greater<char>());
	ss1 << cn;
	ss1 >> intss;
	in = intss;
	for(int j = 0; j < 4; j++){
	  cncp += cn[3 - j];
	}
	ss2 << cncp;
	ss2 >> intss;
	in -= intss;
	ss3 << in;
	ss3 >> cn;
	cout<< cn << endl;
	ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<list>
#include<set>
#include<stack>
using namespace std;
char n[5];
int k[5];

int jk(int sum); // L-Sを返す
int j;

int main(){
  int cnt;
  int sum;
  int fl;
  while(1){

    scanf("%s",n);

    if(n[0]=='0'&&n[1]=='0'&&n[2]=='0'&&n[3]=='0')break;
    fl = 0;
    for(int i=0;i<3;i++){
      if(n[i] != n[i+1]){
        fl = 1;
      }
    }

    //分解
    if(fl == 1){
      sum = 0;
      j = 1000;
      for(int i=0;i<4;i++){
        k[i] = n[i]-'0';
        sum+= k[i]*j;
        j/=10;
      }

      //操作
      cnt = 0;
      while(sum != 6174){
        cnt++;
        sum = jk(sum);
      }
      cout << cnt << endl;
    }else{
      cout << "NA" << endl;
    }
  }

  return 0;
}

int jk(int sum){
  int ls,l=0,s=0;
  int j;
  j = 1000;
  for(int i=0;i<4;i++){
    k[i] = sum/j;
    sum = sum%j;
    j/=10;
  }
  sort(k,k+4);
  j = 1000;
  for(int i=0;i<4;i++){
    s+=k[i] * j;
    j/=10;
  }

  reverse(k,k+4);
  j = 1000;
  for(int i=0;i<4;i++){
    l+=k[i] * j;
    j/=10;
  }

  ls = l - s;

  return ls;
}