#include <cstdio>
#include <vector>
#define dep(i,j,k) for (int (i)=(j);(i)>=(k);--(i))
#define rep(i,j,k) for (int (i)=(j);(i)<=(k);++(i))
using namespace std;
typedef long long ll;
const int C=10,L=20,N=(int)1e5;
vector<int> leaf;
int n,c,h,mm,lim,cnt=1;
int trie[N*20+10][C+10],fa[N*20+10][L+10];
int a[N+10],son[N+10],degree[N+10],ed[N*2+10],nxt[N*2+10];
int b[N*20+10],d[N*20+10],lg[N*20+10],sa[N*20+10],sum[N*20+10],tsa[N*20+10],rank[N*20+10],trank[N*20+10],height[N*20+10];
inline int getint(){
int x=0;
bool flag=false;
char ch=getchar();
while (!(ch>='0' && ch<='9' || ch=='-')) ch=getchar();
if (ch=='-') flag=true,ch=getchar();
while (ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
return flag?-x:x;
}
inline void addedge(int x,int y){
nxt[++mm]=son[x]; son[x]=mm; ed[mm]=y;
nxt[++mm]=son[y]; son[y]=mm; ed[mm]=x;
}
void dfs(int x,int pre,int u){
if (!trie[u][a[x]]) trie[u][a[x]]=++cnt,b[cnt]=a[x];
u=trie[u][a[x]];
for (int i=son[x];i;i=nxt[i])
if (ed[i]!=pre) dfs(ed[i],x,u);
}
void dfs2(int x){
rep(i,1,L) fa[x][i]=fa[fa[x][i-1]][i-1];
rep(i,1,c) if (trie[x][i]){
d[trie[x][i]]=d[x]+1;
fa[trie[x][i]][0]=x;
dfs2(trie[x][i]);
}
}
inline int jump(int x,int y){
for (;y;y-=y&-y) x=fa[x][lg[y&-y]];
return x;
}
void radix(int j){
rep(i,0,lim) sum[i]=0;
rep(i,1,cnt) sum[rank[fa[i][j]]]++;
rep(i,1,lim) sum[i]+=sum[i-1];
dep(i,cnt,1) tsa[sum[rank[fa[i][j]]]--]=i;
rep(i,0,lim) sum[i]=0;
rep(i,1,cnt) sum[rank[tsa[i]]]++;
rep(i,1,lim) sum[i]+=sum[i-1];
dep(i,cnt,1) sa[sum[rank[tsa[i]]]--]=tsa[i];
}
void getsa(){
rep(i,1,cnt) trank[i]=b[i];
rep(i,1,cnt) sum[trank[i]]++;
rep(i,1,c) sum[i]+=sum[i-1];
dep(i,cnt,1) sa[sum[trank[i]]--]=i;
rank[sa[1]]=lim=1;
for (int i=2;i<=cnt;++i){
if (trank[sa[i]]!=trank[sa[i-1]]) lim++;
rank[sa[i]]=lim;
}
for (int j=0;(1<<j)<=cnt && (1<<j)<=h;++j){
radix(j);
trank[sa[1]]=lim=1;
for (int i=2;i<=cnt;++i){
if (rank[sa[i]]!=rank[sa[i-1]] || rank[fa[sa[i]][j]]!=rank[fa[sa[i-1]][j]]) lim++;
trank[sa[i]]=lim;
}
rep(i,1,cnt) rank[i]=trank[i];
}
}
int getheight(int x){
int y=0;
rep(i,1,c) if (trie[x][i]) y=max(y,getheight(trie[x][i]));
if (y) y--;
if (x==1) return 0;
int u=jump(x,y),v=jump(sa[rank[x]-1],y);
while (u>1 && v>1 && b[u]==b[v]) y++,u=fa[u][0],v=fa[v][0];
height[rank[x]]=y;
return y;
}
int main(){
n=getint(); c=getint();
rep(i,1,n) a[i]=getint(),a[i]++;
rep(i,2,n){
int x=getint(),y=getint();
addedge(x,y);
degree[x]++;
degree[y]++;
}
rep(i,1,n) if (degree[i]==1) leaf.push_back(i);
for (vector<int>::iterator i=leaf.begin();i!=leaf.end();++i) dfs(*i,0,1);
rep(i,2,cnt) lg[i]=lg[i-1]+(1<<lg[i-1]+1==i);
dfs2(1);
rep(i,1,cnt) h=max(h,d[i]);
getsa();
getheight(1);
ll ans=0;
rep(i,1,cnt) ans+=d[i];
rep(i,1,cnt) ans-=height[i];
printf("%lld\n",ans);
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPHZlY3Rvcj4KCiNkZWZpbmUgZGVwKGksaixrKSBmb3IgKGludCAoaSk9KGopOyhpKT49KGspOy0tKGkpKQojZGVmaW5lIHJlcChpLGosaykgZm9yIChpbnQgKGkpPShqKTsoaSk8PShrKTsrKyhpKS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