//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <x86intrin.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define V vector
template <typename T> bool chmin(T &a, const T &b) {if(a > b){a = b; return true;} return false;}
template <typename T> bool chmax(T &a, const T &b) {if(a < b){a = b; return true;} return false;}
template<typename T> void print(T a) {ll x = a; cout << x << ' ';}
template<typename A, size_t N, typename T> void Fill (A (&array)[N], const T & val) {fill ((T*)array, (T*)(array+N), val);}
V<ll> dx = {-1, 1,  0, 0, -1, -1,  1, 1};
V<ll> dy = { 0, 0, -1, 1, -1,  1, -1, 1};

ll f (string s) {
    ll n = s.size();
    V<ll> p(n); rep(i, n) p[i] = s[i] - '0';
    sort(allr(p));
    ll g1 = 0;
    ll a = 1;
    for (ll i = n-1; i >= 0; i--) {
        g1 += p[i] * a;
        a *= 10;
    }
    sort(all(p));
    ll g2 = 0;
    a = 1;
    for (ll i = n-1; i >= 0; i--) {
        g2 += p[i] * a;
        a *= 10;
    }
    return g1 - g2;
}

int main () {
    string s; cin >> s;
    ll k; cin >> k;
    if (k == 0) {
        cout << s << endl;
        return 0;
    }
    ll t;
    while (k--) {
        t = f(s);
        s = to_string(t);
    }
    cout << t << endl;
    return 0;
}
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <list>
#include <algorithm>
#define eps 10e-14
//cout<<fixed<<setprecision(15)<<
//s=to_string(100);
//vector<vector<char>>c(h,vector<char>(w));
//next_permutation(orig.begin(),orig.end());
using namespace std;
int gcd(int x,int y){
  while(y>0){ 
    int r=x%y;
      x=y;
      y=r;
  }
  return x;
}
int lcm(int a,int b){
  int r=gcd(a,b);
  return a*b/r;
}
template<typename T, typename U>
void primeFactorization(map<T, U> &primes, T N) {
  T n=N;
  U num=0;
  while(n%2==0){
    num++;
    n/=2;
  }
  if(num!=0){
    primes.emplace(2,num);
  }
  for(T i=3;i*i<=N;i+=2) {
    num=0;
    while(n%i==0){
      num++;
      n/=i;
    }
    if(num!=0){
      primes.emplace(i, num);
    }
  }
  if(n!=1){
    primes.emplace(n,1);
  }
}
template<typename TypeInt>
string Itoa(const TypeInt v, int base){
  static const char table[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  string ret;
  static numeric_limits<TypeInt> t;
  TypeInt n=v;
  if(t.is_signed){
    if(v<0)n*=-1;
  }
  while(n>=base){
    ret+=table[n%base];
    n/=base;
  }
  ret+=table[n];
  if(t.is_signed){
    if(v<0&&base==10)ret+='-';
  }
  reverse(ret.begin(),ret.end());
  return ret;
}
long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
int main(){
 unsigned long long n,a,ans=0;
 map<unsigned long long,unsigned long long>m;
 cin>>n;
 for(int i=0;i<n;i++){
   cin>>a;
   m[a]++;
 }
 ans=(n*(n-1));
 for(auto p:m)ans-=(p.second*(p.second-1));
 cout<<ans/2<<endl;
}