#include <vector>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <stack>
#include <bitset>
using namespace std;
const int NMAX = 100004;
const int VALMAX = 2e6;
vector < int > Tree[NMAX];
int value[NMAX], niv[NMAX] , answer[NMAX];
bitset < VALMAX+6> viz;
int prime[2*NMAX],pos[6+VALMAX];
stack < int > St[2*NMAX];
inline void Eratosthenes(){
prime[++prime[0]] = 2;
pos[2] = 1;
for(int i = 3;i <= VALMAX;i += 2)
if(!viz[i]){
prime[++prime[0]] = i;
pos[i] = prime[0];
for(long long j = 1LL*i*i;j <= VALMAX; j += 2*i)
viz[j] = 1;
}
}
inline void DFS(const int node,const int father){
vector < int > v;
int d = 2,step = 1;
int x = value[node];
answer[node] = 0;
while(d*d <= x){
if(x%d==0){
if(!St[pos[d]].empty()){
int y = St[pos[d]].top();
if(niv[y] > niv[answer[node]])
answer[node] = y;
}
v.push_back(pos[d]);
St[pos[d]].push(node);
while(x%d==0)
x /= d;
}
++step;
d = prime[step];
}
if(x > 1){
if(!St[pos[x]].empty()){
int y = St[pos[x]].top();
if(niv[y] > niv[answer[node]])
answer[node] = y;
}
v.push_back(pos[x]);
St[pos[x]].push(node);
}
for(vector < int > ::iterator it = Tree[node].begin(); it != Tree[node].end(); ++it)
if(*it!=father){
niv[*it] = niv[node] + 1;
DFS(*it,node);
}
for(vector < int > ::iterator it = v.begin(); it != v.end() ; ++it)
St[*it].pop();
v.clear();
}
int main(){
#ifndef ONLINE_JUDGE
freopen("date.in","r",stdin);
freopen("date.ok","w",stdout);
#endif
cin.sync_with_stdio(false);
Eratosthenes();
int n, m;
cin >> n >> m;
for(int i = 1;i <= n; ++i)
cin >> value[i];
for(int i = 1;i < n; ++i){
int x, y;
cin >> x >> y;
Tree[x].push_back(y);
Tree[y].push_back(x);
}
niv[1] = 1;
DFS(1,-1);
while(m--){
int operation, x, y;
cin >> operation >> x;
if(operation == 1){
if(answer[x] > 0)
cout<<answer[x]<<"\n";
else
cout<<"-1\n";
}
else{
cin >> y;
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YoeCVkPT0wKXsKICAgICAgICAgICAgaWYoIVN0W3Bvc1tkXV0uZW1wdHkoKSl7CiAgICAgICAgICAgICAgICBpbnQgeSA9IFN0W3Bvc1tkXV0udG9wKCk7CiAgICAgICAgICAgICAgICBpZihuaXZbeV0gPiBuaXZbYW5zd2VyW25vZGVdXSkKICAgICAgICAgICAgICAgICAgICBhbnN3ZXJbbm9kZV0gPSB5OwogICAgICAgICAgICB9CiAgICAgICAgICAgIHYucHVzaF9iYWNrKHBvc1tkXSk7CiAgICAgICAgICAgIFN0W3Bvc1tkXV0ucHVzaChub2RlKTsKICAgICAgICAgICAgd2hpbGUoeCVkPT0wKQogICAgICAgICAgICAgICAgeCAvPSBkOwogICAgICAgIH0KICAgICAgICArK3N0ZXA7CiAgICAgICAgZCA9IHByaW1lW3N0ZXBdOwogICAgfQogICAgaWYoeCA+IDEpewogICAgICAgIGlmKCFTdFtwb3NbeF1dLmVtcHR5KCkpewogICAgICAgICAgICBpbnQgeSA9IFN0W3Bvc1t4XV0udG9wKCk7CiAgICAgICAgICAgIGlmKG5pdlt5XSA+IG5pdlthbnN3ZXJbbm9kZV1dKQogICAgICAgICAgICAgICAgIGFuc3dlcltub2RlXSA9IHk7CiAgICAgICAgfQogICAgICAgIHYucHVzaF9iYWNrKHBvc1t4XSk7CiAgICAgICAgU3RbcG9zW3hdXS5wdXNoKG5vZGUpOwogICAgfQogICAgZm9yKHZlY3RvciA8IGludCA+IDo6aXRlcmF0b3IgaXQgPSBUcmVlW25vZGVdLmJlZ2luKCk7IGl0ICE9IFRyZWVbbm9kZV0uZW5kKCk7ICsraXQpCiAgICAgICAgaWYoKml0IT1mYXRoZXIpewogICAgICAgICAgICBuaXZbKml0XSA9IG5pdltub2RlXSArIDE7CiAgICAgICAgICAgIERGUygqaXQsbm9kZSk7CiAgICAgICAgfQogICAgZm9yKHZlY3RvciA8IGludCA+IDo6aXRlcmF0b3IgaXQgPSB2LmJlZ2luKCk7IGl0ICE9IHYuZW5kKCkgOyArK2l0KQogICAgICAgIFN0WyppdF0ucG9wKCk7CiAgICB2LmNsZWFyKCk7Cn0KCmludCBtYWluKCl7CiAgICAjaWZuZGVmIE9OTElORV9KVURHRQogICAgICAgIGZyZW9wZW4oImRhdGUuaW4iLCJyIixzdGRpbik7CiAgICAgICAgZnJlb3BlbigiZGF0ZS5vayIsInciLHN0ZG91dCk7CiAgICAjZW5kaWYKICAgIGNpbi5zeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgRXJhdG9zdGhlbmVzKCk7CiAgICBpbnQgbiwgbTsKICAgIGNpbiA+PiBuID4+IG07CiAgICBmb3IoaW50IGkgPSAxO2kgPD0gbjsgKytpKQogICAgICAgIGNpbiA+PiB2YWx1ZVtpXTsKICAgIGZvcihpbnQgaSA9IDE7aSA8IG47ICsraSl7CiAgICAgICAgaW50IHgsIHk7CiAgICAgICAgY2luID4+IHggPj4geTsKICAgICAgICBUcmVlW3hdLnB1c2hfYmFjayh5KTsKICAgICAgICBUcmVlW3ldLnB1c2hfYmFjayh4KTsKICAgIH0KICAgIG5pdlsxXSA9IDE7CiAgICBERlMoMSwtMSk7CiAgICB3aGlsZShtLS0pewogICAgICAgIGludCBvcGVyYXRpb24sIHgsIHk7CiAgICAgICAgY2luID4+IG9wZXJhdGlvbiA+PiB4OwogICAgICAgIGlmKG9wZXJhdGlvbiA9PSAxKXsKICAgICAgICAgICAgaWYoYW5zd2VyW3hdID4gMCkKICAgICAgICAgICAgICAgIGNvdXQ8PGFuc3dlclt4XTw8IlxuIjsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgY291dDw8Ii0xXG4iOwogICAgICAgIH0KICAgICAgICBlbHNlewogICAgICAgICAgICBjaW4gPj4geTsKICAgICAgICAgICAgdmFsdWVbeF0gPSB5OwogICAgICAgICAgICBERlMoMSwtMSk7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIDA7Cn0=