#include <bits/stdc++.h>
using namespace std;
char ch;
bool fh;
inline void read(int &a) {
  for (fh = 0, ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') fh = 1;
  for (a = 0; ch >= '0' && ch <= '9'; ch = getchar()) (a *= 10) += ch - '0';
  if (fh) a = -a;
}
int n;
int val[200010];
struct rec {
  int x, ne;
} Arre[400010];
int Arrmap[200010];
int Atop;
inline void ins(int f, int s) {
  Arre[++Atop].x = s;
  Arre[Atop].ne = Arrmap[f];
  Arrmap[f] = Atop;
}
int ans = 0;
bool Cut[200010];
int vis[200010];
int tim;
int Size[200010];
int pos, posf, area;
void focusDFS(int x) {
  int v, f = 0;
  vis[x] = tim;
  Size[x] = 1;
  for (v = Arrmap[x]; v; v = Arre[v].ne)
    if ((vis[Arre[v].x] != tim) && (!Cut[Arre[v].x])) {
      focusDFS(Arre[v].x);
      Size[x] += Size[Arre[v].x];
      f = max(f, Size[Arre[v].x]);
    }
  f = max(f, area - Size[x]);
  if (f < posf) pos = x, posf = f;
}
inline int gcd(int a, int b) {
  for (int c = a % b; c; a = b, b = c, c = a % b)
    ;
  return b;
}
int Fin[200010];
int Ftop, uplen;
void decompose(int x) {
  int vi;
  uplen = ceil(sqrt(x));
  for (vi = 2; vi <= uplen; vi++)
    if (!(x % vi)) {
      for (; !(x % vi); x /= vi)
        ;
      Fin[++Ftop] = vi;
    }
  if (x != 1) Fin[++Ftop] = x;
}
int dist[2][200010], distvis[2][200010];
int Stk[200010];
int stktop;
bool INstk[200010];
void distDFS(int x, int gys, int step) {
  int v;
  vis[x] = tim;
  decompose(gys);
  for (v = 1; v <= Ftop; v++) {
    ans = max(ans, (dist[0][Fin[v]] - 1) * (distvis[0][Fin[v]] == tim) + step);
    dist[1][Fin[v]] = max(dist[1][Fin[v]] * (distvis[1][Fin[v]] == tim), step);
    distvis[1][Fin[v]] = tim;
    if (!INstk[Fin[v]]) Stk[++stktop] = Fin[v], INstk[Fin[v]] = 1;
  }
  Ftop = 0;
  for (v = Arrmap[x]; v; v = Arre[v].ne)
    if ((vis[Arre[v].x] != tim) && (!Cut[Arre[v].x]))
      distDFS(Arre[v].x, gcd(gys, val[Arre[v].x]), step + 1);
}
void Copy() {
  int x;
  for (; stktop; stktop--) {
    x = Stk[stktop];
    INstk[x] = 0;
    distvis[1][x] = tim - 1;
    dist[0][x] = max(dist[0][x] * (distvis[0][x] == tim), dist[1][x]);
    distvis[0][x] = tim;
    Stk[stktop] = 0;
  }
}
void Div(int root) {
  int v;
  if (area == 1) return;
  tim++;
  posf = area + 1;
  focusDFS(root);
  v = pos;
  tim++;
  posf = area + 1;
  focusDFS(pos);
  pos = v;
  Cut[pos] = 1;
  tim++;
  for (v = Arrmap[pos]; v; v = Arre[v].ne)
    if (!Cut[Arre[v].x]) {
      distDFS(Arre[v].x, gcd(val[pos], val[Arre[v].x]), 2);
      Copy();
    }
  for (v = Arrmap[pos]; v; v = Arre[v].ne)
    if (!Cut[Arre[v].x]) {
      area = Size[Arre[v].x];
      Div(Arre[v].x);
    }
}
int main() {
  int vi, u, v;
  read(n);
  for (vi = 1; vi <= n; vi++) read(val[vi]);
  for (vi = 1; vi < n; vi++) {
    read(u);
    read(v);
    ins(u, v);
    ins(v, u);
  }
  area = n;
  Div(n);
  if (!ans)
    for (vi = 1; vi <= n; vi++)
      if (val[vi] != 1) {
        ans = 1;
        break;
      }
  printf("%d\n", ans);
}
