#include <bits/stdc++.h>
using namespace std;
int u, v, c, a[100005], ans[100005], p[2000006], x, n, q, k, pas[100005],
    level[100005];
vector<int> G[2000006], V[100005];
void dfs(int u, int par) {
  int c = a[u];
  ans[u] = pas[u] = -1;
  while (c > 1) {
    if (p[c] == 0) p[c] = c;
    int d = p[c];
    if (G[d].size()) {
      if (level[G[d].back()] > pas[u])
        pas[u] = level[G[d].back()], ans[u] = G[d].back();
    }
    G[d].push_back(u);
    while (c % d == 0) c /= d;
  }
  for (int i = 0; i < V[u].size(); i++) {
    if (V[u][i] != par) {
      level[V[u][i]] = level[u] + 1;
      dfs(V[u][i], u);
    }
  }
  c = a[u];
  while (c > 1) {
    int d = p[c];
    G[d].pop_back();
    while (c % d == 0) c /= d;
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> q;
  for (k = 2; k * k <= 2000000; k++) {
    if (p[k]) continue;
    for (int i = k * k; i <= 2000000; i += k) {
      if (!p[i]) p[i] = k;
    }
  }
  for (k = 1; k <= n; k++) {
    cin >> a[k];
  }
  for (k = 2; k <= n; k++) {
    cin >> u >> v;
    V[u].push_back(v);
    V[v].push_back(u);
  }
  dfs(1, 0);
  while (q--) {
    cin >> x;
    if (x == 1) {
      cin >> u;
      cout << ans[u] << " ";
    } else {
      cin >> u >> v;
      a[u] = v;
      dfs(1, 0);
    }
  }
}
