#include<cstdio>#include<cstring>#include<algorithm>usingnamespace std;#define N 1000010#define M 100010int n, m, ans =0;int a[N], dp[N], dfn[N], mx[N][2], ms[N][2], f[N][20];int last[N], nxt[N *2], to[N *2], len =0;int b[N], c[N], d[N], la[M], fi[M];intcmp(int x,int y){return dfn[x]< dfn[y];}voidadd(int x,int y){
to[++len]= y;
nxt[len]= last[x];
last[x]= len;}voiddfs(int k,int fa){
f[k][0]= fa;for(int i =1; i <20; i++) f[k][i]= f[f[k][i -1]][i -1];
dp[k]= dp[fa]+1;
dfn[k]=++dfn[0];
mx[k][0]= mx[k][1]=-1;
ms[k][0]= ms[k][1]=0;for(int i = last[k]; i; i = nxt[i])if(to[i]!= fa){int x = to[i];dfs(x, k);if(ms[x][0]+1>= ms[k][0]) mx[k][1]= mx[k][0], ms[k][1]= ms[k][0], mx[k][0]= x, ms[k][0]= ms[x][0]+1;elseif(ms[x][0]+1> ms[k][1]) mx[k][1]= x, ms[k][1]= ms[x][0]+1;}}intlca(int x,int y){if(dp[x]< dp[y])swap(x, y);for(int i =19; i >=0; i--)if(dp[x]-(1<< i)>= dp[y]) x = f[x][i];if(x == y)return x;for(int i =19; i >=0; i--)if(f[x][i]!= f[y][i]) x = f[x][i], y = f[y][i];return f[x][0];}voidsolve(int k,int fa,int t){int s =0;for(int i = last[k]; i; i = nxt[i])if(to[i]!= fa){if(mx[k][0]== to[i])solve(to[i], k,max(t, ms[k][1])+1);elsesolve(to[i], k,max(t, ms[k][0])+1);
c[k]+= c[to[i]];
d[k]+= d[to[i]];}if(c[k]== m) ans =max(ans, t +1);if(d[k]==0) ans =max(ans, ms[k][0]+2);}intread(){int s =0;char x =getchar();while(x <'0'|| x >'9') x =getchar();while(x >='0'&& x <='9') s = s *10+ x -48, x =getchar();return s;}intmain(){int i, x, y;
n =read(), m =read();for(i =1; i <= n; i++) a[i]=read();for(i =1; i < n; i++){
x =read(), y =read();add(x, y),add(y, x);}dfs(1,0);for(i =1; i <= n; i++) b[i]= i;sort(b +1, b + n +1, cmp);for(i =1; i <= n; i++){if(la[a[b[i]]]) c[b[i]]++, c[lca(b[i], la[a[b[i]]])]--;else c[b[i]]++, fi[a[b[i]]]= b[i];
la[a[b[i]]]= b[i];}for(i =1; i <= m; i++) d[lca(fi[i], la[i])]++;solve(1,0,0);printf("%d
", ans);return0;}