#include <bits/stdc++.h>
int primes[] = {2,   3,   5,   7,   11,  13,  17,  19,  23,  29,  31,  37,  41,
                43,  47,  53,  59,  61,  67,  71,  73,  79,  83,  89,  97,  101,
                103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167,
                173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,
                241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,
                317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
                401, 409, 419, 421, 431, 433, 439, 443};
int mgcd = 0;
int n;
std::vector<int> a;
std::vector<std::vector<int>> e;
int work(int v, int p, int d, bool strict) {
  if (d < 2) {
    return 0;
  }
  bool ok = a[v] % d == 0;
  if (ok) {
    while (a[v] % d == 0) {
      a[v] /= d;
    }
  }
  if (strict && !ok) {
    return 0;
  }
  int m1 = 0;
  int m2 = 0;
  std::vector<int> children = e[v];
  for (std::vector<int>::const_iterator i = children.begin();
       i != children.end(); ++i) {
    if (*i != p) {
      int ch = work(*i, v, d, strict);
      if (ch > m2) {
        m1 = m2;
        m2 = ch;
      } else if (ch > m1) {
        m1 = ch;
      }
    }
  }
  if (ok && m1 + m2 + 1 > mgcd) {
    mgcd = m1 + m2 + 1;
  }
  return ok ? m2 + 1 : 0;
}
int main() {
  std::cin >> n;
  a.resize(n);
  e.resize(n);
  for (int i = 0; i < n; ++i) {
    std::cin >> a[i];
  }
  for (int i = 0; i < n - 1; ++i) {
    int x, y;
    std::cin >> x >> y;
    --x;
    --y;
    e[x].push_back(y);
    e[y].push_back(x);
  }
  for (int i = 0; i < sizeof(primes) / sizeof(int); ++i) {
    int d = primes[i];
    work(0, -1, d, false);
  }
  for (int i = 0; i < n; ++i) {
    if (a[i] > 444) {
      work(i, -1, a[i], true);
    }
  }
  std::cout << mgcd << std::endl;
}
