#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
vector<int> g1[MAXN], g2[MAXN], p[MAXN];
int vis[MAXN];
int ar[MAXN];
int prime[MAXN];
int dis[MAXN];
int n, ans, kount = 0;
map<int, int> mp;
inline void init() {
  prime[1] = 1;
  for (long long int i = 2; i < MAXN; i++) {
    if (!prime[i]) {
      prime[i] = i;
      for (long long int j = i * i; j < MAXN; j += i) prime[j] = i;
    }
  }
}
inline int bfs(int src) {
  int ret;
  kount++;
  queue<int> q;
  q.push(src);
  vis[src] = kount;
  dis[src] = 1;
  while (!q.empty()) {
    int f = q.front();
    mp[f]++;
    ret = f;
    q.pop();
    for (auto it : g2[f]) {
      if (vis[it] != kount) {
        vis[it] = kount;
        dis[it] = dis[f] + 1;
        q.push(it);
      }
    }
  }
  return ret;
}
inline void sol(int pr) {
  for (auto it : p[pr]) {
    g2[it].clear();
    for (auto jt : g1[it]) {
      if (ar[jt] % pr == 0) {
        g2[it].push_back(jt);
      }
    }
  }
  mp.clear();
  for (auto it : p[pr]) {
    if (mp.find(it) == mp.end()) {
      int src = bfs(it);
      src = bfs(src);
      ans = max(ans, dis[src]);
    }
  }
}
int main() {
  init();
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", &ar[i]);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    g1[x - 1].push_back(y - 1);
    g1[y - 1].push_back(x - 1);
  }
  for (int i = 0; i < n; i++) {
    int val = ar[i];
    while (val > 1) {
      int pr = prime[val];
      p[pr].push_back(i);
      while (val % pr == 0) val /= pr;
    }
  }
  for (int i = 2; i < MAXN; i++) {
    if (prime[i] == i) {
      sol(i);
    }
  }
  printf("%d\n", ans);
  return 0;
}
