#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1;
int n, a[N], mxnd, mx, ans, fr[N];
bool flg, vis[N];
vector<int> dv[N], g[N];
map<int, vector<pair<int, int>>> mm;
void fct(long long x, int id) {
  for (long long i = 2; i * i <= x; ++i) {
    if (x % i) continue;
    while (x % i == 0ll) x /= i;
    dv[id].push_back(i);
  }
  if (x > 1ll) dv[id].push_back(x);
}
void dfs(int u, int p, int f) {
  vis[u] = true;
  if (f > mx) {
    mxnd = u;
    mx = f;
  }
  for (auto v : g[u])
    if (v != p) dfs(v, u, f + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
    ans = (a[i] > 1);
    fct(a[i], i);
  }
  for (int i = 0, x, y; i < n - 1; ++i) {
    scanf("%d%d", &x, &y);
    --x, --y;
    int z1 = dv[x].size(), z2 = dv[y].size(), j = 0;
    for (auto s : dv[x]) {
      while (j < z2 && dv[y][j] < s) ++j;
      if (j == z2) break;
      if (dv[y][j] == s) mm[s].push_back(make_pair(x, y));
    }
  }
  for (auto x : mm) {
    if (x.second.size() == 0) continue;
    vector<int> ids;
    for (auto s : x.second) {
      g[s.first].push_back(s.second);
      g[s.second].push_back(s.first);
      if (fr[s.first] == 0) ids.push_back(s.first);
      if (fr[s.second] == 0) ids.push_back(s.second);
      fr[s.first] = fr[s.second] = 1;
    }
    for (auto i : ids) {
      if (vis[i]) continue;
      mx = 0, mxnd = i;
      dfs(i, -1, 1);
      mx = 0;
      dfs(mxnd, -1, 1);
      ans = max(ans, mx);
    }
    for (auto i : ids) {
      vis[i] = fr[i] = 0;
      g[i].clear();
    }
  }
  printf("%d\n", ans);
}
