#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 69;
const int MAXP = 2e6 + 69;
int n, q;
int val[MAXN];
vector<int> a[MAXN];
int check[MAXP];
vector<int> p;
void build() {
  for (long long i = 2; i < MAXP; i++) {
    if (check[i] == 0) {
      p.push_back(i);
      check[i] = i;
      for (long long j = i * i; j < MAXP; j += i) {
        if (check[j] == 0) check[j] = i;
      }
    }
  }
}
int ans[MAXN];
int h[MAXN];
vector<int> prim[MAXP];
void ptich(int i) {
  int x = val[i];
  while (check[x] != 0) {
    int t = check[x];
    if (!prim[t].empty()) {
      int u = prim[t].back();
      if (h[u] > h[ans[i]]) {
        ans[i] = u;
      }
    }
    prim[t].push_back(i);
    while (x % t == 0) x /= t;
  }
}
void xoa(int i) {
  int x = val[i];
  while (check[x] != 0) {
    int t = check[x];
    prim[t].pop_back();
    while (x % t == 0) x /= t;
  }
}
void dfs(int x, int p) {
  h[x] = h[p] + 1;
  ptich(x);
  for (int u : a[x]) {
    if (u == p) continue;
    dfs(u, x);
  }
  xoa(x);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  build();
  cin >> n >> q;
  for (int i = 1; i <= n; i++) cin >> val[i];
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    a[x].push_back(y);
    a[y].push_back(x);
  }
  dfs(1, 1);
  while (q--) {
    int type;
    cin >> type;
    if (type == 2) {
      for (int i = 1; i <= n; i++) ans[i] = 0, h[i] = 0;
      for (int i = 1; i < MAXP; i++) prim[i].clear();
      int x, y;
      cin >> x >> y;
      val[x] = y;
      dfs(1, 1);
    } else {
      int x;
      cin >> x;
      if (ans[x] != 0)
        cout << ans[x] << '\n';
      else
        cout << -1 << '\n';
    }
  }
}
