#include<bits/stdc++.h>
using namespace std;
using ll = int64_t;
using P = pair<int,int>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define INF 1e17

class Prime {
// n以下の素数を列挙する
public:
  const int n;
  vector<bool> is_prime;
  vector<int> primes;
  Prime(int size) : n(size), is_prime(n+1, true) {
    is_prime[0] = false;
    is_prime[1] = false;
    for (int i = 2; i <= n; ++i) {
      if (!is_prime[i]) continue;
      primes.push_back(i);
      int tmp = 2*i;
      while (tmp <= n) {
        is_prime[tmp] = false;
        tmp += i;
      }
    }
  }

  bool check(int x) { return is_prime[x]; }
};

int main(){
    ll n;
    cin>>n;
    ll x[n];
    rep(i,n){
        cin>>x[i];
    }
    vector<set<ll>> yakusu(n);
    vector<int> primes;
    rep(i,n){
        for(ll j=1;j*j<=x[i];j++){
            if(x[i]%j==0){
                if(j!=1)yakusu[i].insert(j);
                yakusu[i].insert(x[i]/j);
            }
        }
    }
    Prime prime(50);
  for (int p : prime.primes) {
    primes.push_back(p);
  }
  int num = primes.size();
  ll ans = INF;
  for(int i=1;i<1<<num;i++){
    ll koho = 1;
      for(int j=0;j<num;j++){
          if(i&1<<j)koho*=primes[j];
      }
      bool sodenai = true;
      for(int k=0;k<n;k++){
          bool check = false;
          for(auto di:yakusu[k]){
              if(koho%di==0)check=true;
          }
          if(!check)sodenai=false;
      }
      if(sodenai){
          if(ans==INF)ans=koho;
          if(ans>koho)ans=koho;
      }
  }
  cout<<ans<<endl;
}
#include<algorithm>
#include<cstdio>
#include<map>
typedef long long ll;
std::map<ll,ll>mp;
ll n,k;
ll now;
bool looped;
int zc[18],zcnt;
ll get(ll x,bool rev){
	zcnt=0;
	while(x){
		zc[++zcnt]=x%10;
		x/=10;
	}
	std::sort(zc+1,zc+zcnt+1);
	if(rev)std::reverse(zc+1,zc+zcnt+1);
	ll res=0;
	for(int i=zcnt;i;i--)res=(res<<3)+(res<<1)+zc[i];
	return res;
}
signed main(){
	mp.clear();
	scanf("%lld%lld",&n,&k);
	now=n;
	mp[now]=k;
	while(k){
//		printf("k=%lld\n",k);
		k--;
		now=get(now,false)-get(now,true);
//		puts("ok");
		if(mp[now]&&!looped){
			ll len=k-mp[now];
			k%=len;
			looped=true;
		}
		mp[now]=k;
	}
	printf("%lld",now);
	return 0;
}