#include <bits/stdc++.h>

using namespace std;

int a[11], k, cnt;

int main() {

	int aa = 0;

	char c;
	while((c = getchar()) != ' ') a[++cnt] = c - 48;
	for(int i = 1; i <= cnt; i ++) aa = aa * 10 + a[i];

	cin >> k;

	int ne = aa;

	for(int i = 1; i <= k; i ++) {
		sort(a + 1, a + cnt + 1);
		int b = 0, e = 0;
		for(int i = 1; i <= cnt; i ++) b = b * 10 + a[i];
		reverse(a + 1, a + cnt + 1);
		for(int i = 1; i <= cnt; i ++) e = e * 10 + a[i];

		ne = abs(b - e);

		cnt = 0;

		if(i < k) while(ne > 0) a[++cnt] = ne % 10, ne /= 10;

	}

	cout << ne << endl;
	
	return 0;

}
#include<bits/stdc++.h> 
#define fp(x,a,b) for(ll x=a; x<b; x++)
#define fn(x,a,b) for(int x=a; x>b; x--)
#define f(x, m) for(auto x : m)
#define cpu() ios::sync_with_stdio(false); cin.tie(nullptr)
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int ,int>>
#define vll vector<pair<ll ,ll>>
#define all(v) v.begin(),v.end()
#define sor(a) sort( a.begin(), a.end() )
#define ros(a) sort( a.rbegin(), a.rend())
#define prec(n) fixed<<setprecision(n)
#define ff first
#define ss second
// #define tt third
#define print(x) for(auto it:x) cout<<it<<" ";
#define debug(x) cerr << #x << " is " << x << endl;
#define w(x) while(x--)
#define r(x) scanf("%d", &x)
#define rl(x) scanf("%ld", &x) 
#define rll(x) scanf("%lld", &x)

typedef long long ll;
using namespace std;
template <typename TYPE> void readint(TYPE &x){
x=0; int f=1; char c;
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
for(;isdigit(c);c=getchar())x=x*10+c-'0';
x*=f;
}
#define dbg(args...){ string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
template<typename... T>
void rd(T& ... args){
  ((cin >> args), ...);
}
template<typename... T>
void ps(T ... args){
  ((cout << args << ' '), ...);
  cout<<'\n';
}
const ll MOD = 1e9 + 7, MOD1 = 998244353LL, MAX = 2e5 + 5;
const char nl = '\n';
const int INF = 2e9;
int n, q;
char x[MAX];
//queue front back push pop
//stack top push pop
// __builtin_popcount() : Cuenta todos los bits 1 de un número
// __builtin_parity() : Revisa la paridad de un número
// __builtin_clz() : Cuenta todos los bits 0 a la izquierda del primer bit 1 de un número.
// __builtin_ctz() : Cuenta todos los bits 0 a la derecha del último bit 1 de un número.

void solve(){
  // be careful with typos and overflows, but also with the sizes of the arrays ::: 
	int a[3] = {1, 1, 0};
	rd(n);
	ll ans = 10000000000LL, ga = -1, gb = -1;
	fp(i, 0, n) rd(x[i]);
	if(n == 1){
		ps(ans * (x[0] - '/'));
		return;
	}
	bool gd = false;
	fp(i, 0, 3){
		bool f = true;
		fp(j, 0, n){
			if(x[j] - '0' != a[(j + i) % 3]){
				f = false; 
				break;
			}
		}
		if(f) gd = f, ga = i, gb = (i + n - 1) % 3;
	}
	// dbg(n, ga, gb);
	if(gd) ps(ans - (n + (2 - gb) + ga) / 3 + 1);
	else ps(0);



}  
int main(){
cpu();
// int __;
//  cin>>__;
//  while(__--){
//   solve();
//   // cout << nl;
//  }
   solve();
// cout<<nl;
 
return 0;
}