#include <bits/stdc++.h>
#define ld long double
#define ll long long
#define pb push_back
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size())
using namespace std;
const int MAXN=200000;
int n,k;
int lca[20][500000];
vector<vector<int>> g(MAXN);
vector<set<int>> unvis(MAXN);
vector<int> eul,lvl(MAXN),in(MAXN),par(MAXN),color,colorLCA,dsu,dsu2,sz,highestV,doneColors(MAXN);
vector<int> vis(MAXN);
int getp(int x)
{
if(x==dsu[x]) return x;
return dsu[x]=getp(dsu[x]);
}
void connect(int u, int v)
{
u=getp(u), v=getp(v);
if(u==v) return;
if(sz[v]>sz[u]) swap(u,v);
dsu[v]=u;
sz[u]+=sz[v];
doneColors[u]+=doneColors[v];
}
int getp2(int x)
{
if(x==dsu2[x]) return x;
return dsu2[x]=getp2(dsu2[x]);
}
void connect2(int u, int v)
{
u=getp2(u), v=getp2(v);
if(u==v) return;
if(rand()&1) swap(u,v);
dsu2[v]=u;
if(lvl[highestV[u]]>lvl[highestV[v]]) highestV[u]=highestV[v];
}
void makeeuler(int u, int pr, int d)
{
par[u]=pr;
in[u]=sz(eul);
lvl[u]=d;
eul.pb(u);
for(int v:g[u])
{
if(v==pr) continue;
makeeuler(v,u,d+1);
eul.pb(u);
}
}
int getlca(int u, int v)
{
int l=in[u], r=in[v];
if(l>r) swap(l,r);
int lg=log2(r-l+1);
return (lvl[lca[lg][l]]<=lvl[lca[lg][r-(1<<lg)+1]]?lca[lg][l]:lca[lg][r-(1<<lg)+1]);
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
cin>>n>>k;
for(int i=0;i<k;i++) dsu.pb(i), sz.pb(1);
for(int i=0;i<n-1;i++)
{
int u,v; cin>>u>>v;
u--;v--;
g[u].pb(v);
g[v].pb(u);
}
makeeuler(0,-1,0);
for(int i=0;i<sz(eul);i++) lca[0][i]=eul[i];
for(int i=1;i<20;i++)
for(int j=0;j+(1<<i)<=sz(eul);j++)
lca[i][j] = (lvl[lca[i-1][j]]<=lvl[lca[i-1][j+(1<<(i-1))]]?lca[i-1][j]:lca[i-1][j+(1<<(i-1))]);
for(int i=0;i<n;i++)
{
dsu2.pb(i), highestV.pb(i);
int c; cin>>c; c--;
color.pb(c);
unvis[c].insert(i);
}
int ans=1e9;
set<pair<int,int>> q;
for(int i=0;i<k;i++)
{
int lc=*unvis[i].begin();
for(int v:unvis[i])
lc=getlca(lc,v);
colorLCA.pb(lc);
q.insert({-lvl[lc],lc});
}
for(auto p:q)
{
int candidLCA=p.y;
if(candidLCA!=colorLCA[color[candidLCA]]) continue;
queue<int> qu;
for(int v:unvis[color[candidLCA]])
{
qu.push(v);
if(v!=candidLCA) vis[v]=1;
}
unvis[color[candidLCA]].clear();
doneColors[getp(color[candidLCA])]++;
while(!qu.empty())
{
int v=qu.front(); qu.pop();
while(v!=candidLCA)
{
if(vis[v]==2) break;
connect(color[candidLCA],color[v]);
if(lvl[colorLCA[color[v]]]>=lvl[candidLCA])
{
int bef=sz(unvis[color[v]]);
unvis[color[v]].erase(v);
if(sz(unvis[color[v]])==0 && bef==1) doneColors[getp(color[v])]++;
vis[v]=2;
auto it=unvis[color[v]].begin();
while(it!=unvis[color[v]].end())
{
int t=*it;
if(vis[t])
{
it++;
continue;
}
vis[t]=1;
qu.push(t);
it=unvis[color[t]].erase(it);
if(sz(unvis[color[t]])==0) doneColors[getp(color[t])]++;
}
}
else break;
connect2(v,par[v]);
v=highestV[getp2(par[v])];
if(vis[v]==1) break;
}
}
int rep=getp(color[candidLCA]);
if(doneColors[rep]==sz[rep]) ans=min(ans,sz[rep]-1);
}
cout<<ans<<'\n';
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CgojZGVmaW5lIGxkIGxvbmcgZG91YmxlCiNkZWZpbmUgbGwgbG9uZyBsb25nCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgeCBmaXJzdAojZGVmaW5lIHkgc2Vjb25kCiNkZWZpbmUgYWxsKHgpIHguYmVnaW4oKSx4L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pZ2hlc3RWW3ZdOwp9CnZvaWQgbWFrZWV1bGVyKGludCB1LCBpbnQgcHIsIGludCBkKQp7CiAgICBwYXJbdV09cHI7CiAgICBpblt1XT1zeihldWwpOwogICAgbHZsW3VdPWQ7CiAgICBldWwucGIodSk7CiAgICBmb3IoaW50IHY6Z1t1XSkKICAgIHsKICAgICAgICBpZih2PT1wcikgY29udGludWU7CiAgICAgICAgbWFrZWV1bGVyKHYsdSxkKzEpOwogICAgICAgIGV1bC5wYih1KTsKICAgIH0KfQoKaW50IGdldGxjYShpbnQgdSwgaW50IHYpCnsKICAgIGludCBsPWluW3VdLCByPWluW3ZdOwogICAgaWYobD5yKSBzd2FwKGwscik7CiAgICBpbnQgbGc9bG9nMihyLWwrMSk7CiAgICByZXR1cm4gKGx2bFtsY2FbbGddW2xdXTw9bHZsW2xjYVtsZ11bci0oMTw8bGcpKzFdXT9sY2FbbGddW2xdOmxjYVtsZ11bci0oMTw8bGcpKzFdKTsKfQoKaW50IG1haW4oKQp7CiAgICBpb3M6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsKICAgIGNpbj4+bj4+azsKICAgIGZvcihpbnQgaT0wO2k8aztpKyspIGRzdS5wYihpKSwgc3oucGIoMSk7CiAgICBmb3IoaW50IGk9MDtpPG4tMTtpKyspCiAgICB7CiAgICAgICAgaW50IHUsdjsgY2luPj51Pj52OwogICAgICAgIHUtLTt2LS07CiAgICAgICAgZ1t1XS5wYih2KTsKICAgICAgICBnW3ZdLnBiKHUpOwogICAgfQogICAgbWFrZWV1bGVyKDAsLTEsMCk7CiAgICBmb3IoaW50IGk9MDtpPHN6KGV1bCk7aSsrKSBsY2FbMF1baV09ZXVsW2ldOwogICAgZm9yKGludCBpPTE7aTwyMDtpKyspCiAgICAgICAgZm9yKGludCBqPTA7aisoMTw8aSk8PXN6KGV1bCk7aisrKQogICAgICAgICAgICBsY2FbaV1bal0gPSAobHZsW2xjYVtpLTFdW2pdXTw9bHZsW2xjYVtpLTFdW2orKDE8PChpLTEpKV1dP2xjYVtpLTFdW2pdOmxjYVtpLTFdW2orKDE8PChpLTEpKV0pOwogICAgZm9yKGludCBpPTA7aTxuO2krKykKICAgIHsKICAgICAgICBkc3UyLnBiKGkpLCBoaWdoZXN0Vi5wYihpKTsKICAgICAgICBpbnQgYzsgY2luPj5jOyBjLS07CiAgICAgICAgY29sb3IucGIoYyk7CiAgICAgICAgdW52aXNbY10uaW5zZXJ0KGkpOwogICAgfQogICAgaW50IGFucz0xZTk7CiAgICBzZXQ8cGFpcjxpbnQsaW50Pj4gcTsKICAgIGZvcihpbnQgaT0wO2k8aztpKyspCiAgICB7CiAgICAgICAgaW50IGxjPSp1bnZpc1tpXS5iZWdpbigpOwogICAgICAgIGZvcihpbnQgdjp1bnZpc1tpXSkKICAgICAgICAgICAgbGM9Z2V0bGNhKGxjLHYpOwogICAgICAgIGNvbG9yTENBLnBiKGxjKTsKICAgICAgICBxLmluc2VydCh7LWx2bFtsY10sbGN9KTsKICAgIH0KICAgIGZvcihhdXRvIHA6cSkKICAgIHsKICAgICAgICBpbnQgY2FuZGlkTENBPXAueTsKICAgICAgICBpZihjYW5kaWRMQ0EhPWNvbG9yTENBW2NvbG9yW2NhbmRpZExDQV1dKSBjb250aW51ZTsKICAgICAgICBxdWV1ZTxpbnQ+IHF1OwogICAgICAgIGZvcihpbnQgdjp1bnZpc1tjb2xvcltjYW5kaWRMQ0FdXSkKICAgICAgICB7CiAgICAgICAgICAgIHF1LnB1c2godik7CiAgICAgICAgICAgIGlmKHYhPWNhbmRpZExDQSkgdmlzW3ZdPTE7CiAgICAgICAgfQogICAgICAgIHVudmlzW2NvbG9yW2NhbmRpZExDQV1dLmNsZWFyKCk7CiAgICAgICAgZG9uZUNvbG9yc1tnZXRwKGNvbG9yW2NhbmRpZExDQV0pXSsrOwogICAgICAgIHdoaWxlKCFxdS5lbXB0eSgpKQogICAgICAgIHsKICAgICAgICAgICAgaW50IHY9cXUuZnJvbnQoKTsgcXUucG9wKCk7CiAgICAgICAgICAgIHdoaWxlKHYhPWNhbmRpZExDQSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYodmlzW3ZdPT0yKSBicmVhazsKICAgICAgICAgICAgICAgIGNvbm5lY3QoY29sb3JbY2FuZGlkTENBXSxjb2xvclt2XSk7CiAgICAgICAgICAgICAgICBpZihsdmxbY29sb3JMQ0FbY29sb3Jbdl1dXT49bHZsW2NhbmRpZExDQV0pCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaW50IGJlZj1zeih1bnZpc1tjb2xvclt2XV0pOwogICAgICAgICAgICAgICAgICAgIHVudmlzW2NvbG9yW3ZdXS5lcmFzZSh2KTsKICAgICAgICAgICAgICAgICAgICBpZihzeih1bnZpc1tjb2xvclt2XV0pPT0wICYmIGJlZj09MSkgZG9uZUNvbG9yc1tnZXRwKGNvbG9yW3ZdKV0rKzsKICAgICAgICAgICAgICAgICAgICB2aXNbdl09MjsKICAgICAgICAgICAgICAgICAgICBhdXRvIGl0PXVudmlzW2NvbG9yW3ZdXS5iZWdpbigpOwogICAgICAgICAgICAgICAgICAgIHdoaWxlKGl0IT11bnZpc1tjb2xvclt2XV0uZW5kKCkpCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBpbnQgdD0qaXQ7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmKHZpc1t0XSkKICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgaXQrKzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIHZpc1t0XT0xOwogICAgICAgICAgICAgICAgICAgICAgICBxdS5wdXNoKHQpOwogICAgICAgICAgICAgICAgICAgICAgICBpdD11bnZpc1tjb2xvclt0XV0uZXJhc2UoaXQpOwogICAgICAgICAgICAgICAgICAgICAgICBpZihzeih1bnZpc1tjb2xvclt0XV0pPT0wKSBkb25lQ29sb3JzW2dldHAoY29sb3JbdF0pXSsrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UgYnJlYWs7CiAgICAgICAgICAgICAgICBjb25uZWN0Mih2LHBhclt2XSk7CiAgICAgICAgICAgICAgICB2PWhpZ2hlc3RWW2dldHAyKHBhclt2XSldOwogICAgICAgICAgICAgICAgaWYodmlzW3ZdPT0xKSBicmVhazsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBpbnQgcmVwPWdldHAoY29sb3JbY2FuZGlkTENBXSk7CiAgICAgICAgaWYoZG9uZUNvbG9yc1tyZXBdPT1zeltyZXBdKSBhbnM9bWluKGFucyxzeltyZXBdLTEpOwogICAgfQogICAgY291dDw8YW5zPDwnXG4nOwogICAgcmV0dXJuIDA7Cn0K