#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int MAXC = 10, MAXN = (int) 1e5, MAXV = MAXN * 20;
struct edge {
int to;
edge *next;
};
edge *son[MAXN + 10], e[MAXN * 2 + 10], *edge_cnt = e;
inline void add_edge(int x, int y) {
edge_cnt++;
edge_cnt->to = y;
edge_cnt->next = son[x];
son[x] = edge_cnt;
}
struct node {
int value;
node *parent, *trans[MAXC];
};
node pool[MAXV * 2 + 10], *node_cnt = pool;
inline node* new_node(int _value) {
node *x = node_cnt++;
x->value = _value;
return x;
}
node *root = new_node(0);
node* extend(node *p, int x) {
node *np = new_node(p->value + 1);
while (p && p->trans[x] == NULL)
p->trans[x] = np, p = p->parent;
if (p == NULL)
np->parent = root;
else {
node *q = p->trans[x];
if (p->value + 1 == q->value)
np->parent = q;
else {
node *nq = new_node(p->value + 1);
memcpy(nq->trans, q->trans, sizeof q->trans);
nq->parent = q->parent;
q->parent = nq;
np->parent = nq;
while (p && p->trans[x] == q)
p->trans[x] = nq, p = p->parent;
}
}
return np;
}
int n, c, a[MAXN + 10], degree[MAXN + 10];
void dfs(int x, int pre, node *last) {
node *temp = extend(last, a[x]);
for (edge *i = son[x]; i; i = i->next)
if (i->to != pre)
dfs(i->to, x, temp);
}
int main() {
scanf("%d%d", &n, &c);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
for (int i = 2; i <= n; ++i) {
int x, y;
scanf("%d%d", &x, &y);
add_edge(x, y);
add_edge(y, x);
degree[x]++;
degree[y]++;
}
for (int i = 1; i <= n; ++i)
if (degree[i] == 1)
dfs(i, 0, root);
ll ans = 0;
for (node* i = pool; i != node_cnt; ++i)
if (i->parent)
ans += i->value - i->parent->value;
cout << ans << '\n';
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdHJpbmc+CiNpbmNsdWRlIDxpb3N0cmVhbT4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcg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