#include <bits/stdc++.h>
using namespace std;
int n;
int ans = 0;
int num[200005];
int hei[200005];
int mpf[200005];
int ls[200005], cnt[200005];
vector<int> primes;
vector<int> adj[200005];
unordered_set<int> facts[200005];
unordered_map<int, int> inlst;
unordered_map<int, int> mxh[200005];
void linear_sieve() {
  for (int i = 2; i < 200005; i++) {
    if (!mpf[i]) {
      primes.push_back(i);
      mpf[i] = i;
      facts[i].insert(i);
    }
    vector<int> fcts;
    for (auto x : facts[i]) fcts.push_back(x);
    for (auto x : primes) {
      if (x > mpf[i] || i * x >= 200005) break;
      int m = i * x;
      mpf[m] = x;
      for (auto y : fcts) facts[m].insert(y);
      facts[m].insert(x);
    }
  }
}
void dfs(int st, int pr, int h) {
  hei[st] = h;
  unordered_map<int, pair<int, int> > clst;
  for (auto x : facts[num[st]]) {
    mxh[st][x] = h;
  }
  for (auto x : adj[st]) {
    if (x == pr) continue;
    dfs(x, st, h + 1);
    for (auto y : facts[num[st]]) {
      if (!mxh[x].count(y)) continue;
      if (clst[y].first < mxh[x][y]) {
        clst[y].second = clst[y].first;
        clst[y].first = mxh[x][y];
      } else if (clst[y].second < mxh[x][y]) {
        clst[y].second = mxh[x][y];
      }
      mxh[st][y] = max(mxh[st][y], mxh[x][y]);
    }
  }
  for (auto x : facts[num[st]]) {
    ans = max(ans, mxh[st][x] - h + 1);
    if (clst[x].second)
      ans = max(ans, clst[x].first + clst[x].second - (h << 1) + 1);
  }
}
int main() {
  linear_sieve();
  scanf("%d", &n);
  int ors = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &num[i]);
    ors |= num[i];
  }
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  if (ors == 1) {
    puts("0");
    return 0;
  }
  dfs(1, 0, 1);
  printf("%d\n", ans);
  return 0;
}
