#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
  string N;
  int K;
  cin >> N >> K;
  long A = stoi(N);
  rep(i, K) {
    string J = to_string(A);
    string G, H;
    sort(J.begin(), J.end());
    H = J;
    reverse(J.begin(), J.end());
    G = J;
    A = stoi(G)-stoi(H);
  }
  cout << A << endl;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long

ll n,m,cnt,ans = -0x3f3f3f3f;
ll head[200005];
ll val[200005],maxx[200005],minn[200005];
int vis[200005];

struct K{
	ll val,num;
}p[200005];

struct P{
	ll to,next;
}e[400005];

void add(ll x,ll y){
	e[++cnt].to = y;
	e[cnt].next = head[x];
	head[x] = cnt;
}

void dfs(ll now){
	//std::cout<<now<<" "<<val[now]<<" "<<minn[now]<<std::endl;
	ans = std::max(val[now] - minn[now],ans);
	for(int i = head[now];i;i = e[i].next){
		if(!vis[e[i].to]){
		minn[e[i].to] = std::min(minn[now],val[now]);
		vis[e[i].to] ++ ; 
		dfs(e[i].to);
		}
	}
}

bool operator < (K a,K b){
	return a.val < b.val;
}

int main(){
	memset(maxx,-0x3f,sizeof(maxx));
	memset(minn,0x3f,sizeof(minn));
	scanf("%lld%lld",&n,&m);
	for(int i = 1;i <= n;++i)
	scanf("%lld",&p[i].val),p[i].num = i,val[i] = p[i].val;
	for(int i = 1;i <= m;++i){
		ll x,y;
		scanf("%lld%lld",&x,&y);
		add(x,y); 
	}
	std::sort(p + 1,p + 1 + n);
	for(int i = 1; i<= n;++i)
	if(!vis[p[i].num])
	dfs(p[i].num);
	std::cout<<ans<<std::endl;
}


