#include <bits/stdc++.h>
using namespace std;
const int p = 997;
const int base = 1e9 + 7;
const int MAXN = 2e5 + 10;
const int INF = 1e9;
const int MOD = 1e9;
const double PII = 3.14159265358979323846264338327950288419716939937510;
const double Epsilon = 1.19209e-4;
const double INFLD = 1e9;
const double fi = 2.6180339887498948482;
long long one = 2147483647;
long long two = 1e9 + 7;
vector<vector<int>> graph(MAXN);
vector<int> value(MAXN, 1);
vector<map<int, int>> dp(MAXN);
int answer = 0;
vector<int> get_del(int a) {
  int i = 2;
  vector<int> res;
  if (a <= 1) return res;
  res.push_back(a);
  while (a > 1 && i * i <= a) {
    if (a % i == 0) {
      res.push_back(i);
      while (a % i == 0) {
        a /= i;
      }
    }
    ++i;
  }
  return res;
}
void dfs(int v, int p, int h) {
  vector<int> gg = get_del(value[v]);
  for (int i = 0; i < gg.size(); ++i) {
    dp[v][gg[i]] = h;
    answer = max(answer, 1);
  }
  map<int, int> deti;
  for (int i = 0; i < graph[v].size(); ++i) {
    if (graph[v][i] != p) {
      dfs(graph[v][i], v, h + 1);
      for (auto it : dp[graph[v][i]]) {
        if (dp[v].find(it.first) != dp[v].end()) {
          answer =
              max(answer, abs(h - dp[v][it.first]) + abs(h - it.second) + 1);
          dp[v][it.first] = max(dp[v][it.first], it.second);
        }
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  ;
  int n, l = 0, r = 0, a, b;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> value[i];
  }
  for (int i = 0; i < n - 1; ++i) {
    cin >> a >> b;
    --a;
    --b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  dfs(0, -1, 0);
  cout << answer;
  return 0;
}
