#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int N = 2 * 1e5;
vector<vector<int>> g;
vector<int> a;
vector<unordered_map<int, int>> dp;
int ans;
void dfs(int from, int prev = -1) {
  for (int to : g[from]) {
    if (to != prev) {
      dfs(to, from);
    }
  }
  vector<int> primes;
  for (int i = 2; i * i <= a[from]; ++i) {
    if (a[from] % i == 0) {
      primes.push_back(i);
      while (a[from] % i == 0) {
        a[from] /= i;
      }
    }
  }
  if (a[from] > 1) {
    primes.push_back(a[from]);
  }
  for (int p : primes) {
    vector<int> v(2, 0);
    for (int to : g[from]) {
      v.push_back(dp[to][p]);
    }
    sort(v.begin(), v.end(), greater<int>());
    ans = max(ans, v[0] + v[1] + 1);
    dp[from][p] = v[0] + 1;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  g.resize(n);
  a.resize(n);
  dp.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(0);
  cout << ans;
  return 0;
}
