#include <bits/stdc++.h>
using namespace std;

#define MAX 1000005
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define REP(i, a, b) for(int i = a; i >= b; i--)

int n, m;
int a[MAX], bit[MAX];

void update(int u, int v) {
    while(u <= n) {
        bit[u] += v;
        u += u & -u;
    }
}

int get(int u) {
    int ans = 0;
    while(u > 0) {
        ans += bit[u];
        u -= u & -u;
    }
    return ans;
}

int main() {
    //freopen("main.inp", "r", stdin);
    cin >> n >> m;
    FOR(i, 1, n) cin >> a[i];
    sort(a + 1, a + n + 1);
    FOR(i, 1, n) update(i, 1);
    FOR(i, 1, m) {
        int x;
        cin >> x;
        if(x < 0) {
            x = -x;
            int lo = 1, hi = n;
            while(hi - lo > 1) {
                int mid = (hi + lo)/2;
                if(get(mid) < x) lo = mid;
                else hi = mid;
            }
            update(lo, -1);
        } else update(x, 1);
    }
    FOR(i, 1, n) {
        if(get(i) > 0) {
            cout << a[i];
            return 0;
        }
    }
    cout << 0;
}


#include <bits/stdc++.h>
using namespace std;

#define MAX 1000005
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define REP(i, a, b) for(int i = a; i >= b; i--)

struct Tnode {int stop, cnt, parent, child[2];};
Tnode trie[MAX];
int n, m, nt = 0, root = 0;
int a[MAX], b[MAX];

int read() {
    char c;
    do c = getchar();
    while(c != '-' && (c < '0' || c > '9'));
    int sign = (c == '-') ? -1 : 1;
    int val = (c == '-') ? 0 : c - '0';
    c = getchar();
    while('0' <= c && c <= '9') {
        val = 10 * val + c - '0';
        c = getchar();
    }
    return val * sign;
}

void write(int x) {
    if(x < 10) {putchar(x + '0'); return;}
    write(x / 10);
    putchar(x % 10 + '0');
}

int addnode() {++ nt; return nt;}
void addnum(int x) {
    int r = root;
    REP(k, 20, 0) {
        int i = (x >> k) & 1;
        if(trie[r].child[i] == 0) {
            int l = addnode();
            trie[r].child[i] = l;
            trie[l].parent = r;
        }
        r = trie[r].child[i];
    }
    ++ trie[r].stop;
    ++ trie[r].cnt;
    while(r != root) {
        r = trie[r].parent;
        ++trie[r].cnt;
    }
}

void delnum(int x) {
    int r = root;
    REP(k, 20, 0) {
        int i = (x >> k) & 1;
        if(trie[r].child[i] == 0) return;
        r = trie[r].child[i];
    }
    if(trie[r].stop == 0) return;
    -- trie[r].stop;
    -- trie[r].cnt;
    while(r != root) {
        r = trie[r].parent;
        -- trie[r].cnt;
    }
}

int p_th(int p) {
    int r = root;
    int x = 0;
    REP(k, 20, 0) {
        int i = trie[r].child[0];
        if(p > trie[i].cnt) {
            x |= (1 << k);
            p -= trie[i].cnt;
            r = trie[r].child[1];
        } else r = trie[r].child[0];
    }
    return x;
}

int main() {
    //freopen("main.inp", "r", stdin);
    n = read(), m = read();
    FOR(i, 1, n) a[i] = read();
    FOR(i, 1, m) b[i] = read();
    root = addnode();
    FOR(i, 1, n) addnum(a[i]);
    FOR(i, 1, m) {
        if(b[i] > 0) addnum(p_th(b[i]));
        else delnum(p_th(abs(b[i])));
    }
    if(p_th(1) > 0) {
        write(p_th(1));
        return 0;
    }
    puts("0");
}


#include <bits/stdc++.h>
using namespace std;

#define MAX 1000005
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define REP(i, a, b) for(int i = a; i >= b; i--)

int n, m;
int a[MAX], bit[MAX];

void update(int u, int v) {
    while(u <= n) {
        bit[u] += v;
        u += u & -u;
    }
}

int get(int u) {
    int ans = 0;
    while(u > 0) {
        ans += bit[u];
        u -= u & -u;
    }
    return ans;
}

int main() {
    //freopen("main.inp", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    FOR(i, 1, n) cin >> a[i];
    sort(a + 1, a + n + 1);
    FOR(i, 1, n) update(i, 1);
    FOR(i, 1, m) {
        int x;
        cin >> x;
        if(x < 0) {
            x = -x;
            int lo = 0, hi = n;
            while(hi - lo > 1) {
                int mid = (hi + lo)/2;
                if(get(mid) < x) lo = mid;
                else hi = mid;
            }
            update(hi, -1);
        } else update(x, 1);
    }
    FOR(i, 1, n) {
        if(bit[i] > 0) {
            cout << i;
            return 0;
        }
    }
    cout << 0;
}


#include <bits/stdc++.h>
using namespace std;

#define MAX 1000005
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define REP(i, a, b) for(int i = a; i >= b; i--)

int n, q;
int a[MAX], b[MAX];

void update(int u, int v) {
    while(u <= n) {
       b[u] += v;
       u += u & -u;
    }
}

int get(int u) {
    int ans = 0;
    while(u > 0) {
        ans += b[u];
        u -= u & -u;
    }
    return ans;
}

int main() {
    //freopen("main.inp", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> q;
    FOR(i, 1, n) cin >> a[i];
    FOR(i, 1, n)
        update(a[i], 1);
    FOR(i, 1, q) {
        int k;
        cin >> k;
        if(k > 0) {update(k, 1); continue;}
        k = -k;
        int lo = 0, hi = (int) n;
        while(hi - lo > 1) {
            int mid = (hi + lo)/2;
            if(get(mid) >= k) hi = mid;
            else lo = mid;
        }
        update(hi, -1);
    }
    if(get(n) <= 0) {cout << 0; return 0;}
    FOR(i, 1, n) if(get(i) > 0) {cout << i; return 0;}
}


#include <bits/stdc++.h>
using namespace std;

#define MAX 1000005
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define REP(i, a, b) for(int i = a; i >= b; i--)

int n, m;
int a[MAX], bit[MAX];

void update(int u, int v) {
    while(u <= n) {
        bit[u] += v;
        u += u & -u;
    }
}

int get(int u) {
    int ans = 0;
    while(u > 0) {
        ans += bit[u];
        u -= u & -u;
    }
    return ans;
}

int main() {
    //freopen("main.inp", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    FOR(i, 1, n) cin >> a[i];
    sort(a + 1, a + n + 1);
    FOR(i, 1, n) update(i, 1);
    FOR(i, 1, m) {
        int x;
        cin >> x;
        if(x < 0) {
            x = -x;
            int lo = 1, hi = n;
            while(hi - lo > 1) {
                int mid = (hi + lo)/2;
                if(get(mid) < x) lo = mid;
                else hi = mid;
            }
            update(lo, -1);
        } else update(x, 1);
    }
    FOR(i, 1, n) {
        if(get(i) > 0) {
            cout << a[i];
            return 0;
        }
    }
    cout << 0;
}


#include <bits/stdc++.h>
using namespace std;

#define MAX 1000005
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define REP(i, a, b) for(int i = a; i >= b; i--)

struct Tnode {int stop, cnt, parent, child[2];};
Tnode trie[100005];
int n, m, nt = 0, root = 0;
int a[MAX];

int read() {
    char c;
    do c = getchar();
    while(c != '-' && (c < '0' || c > '9'));
    int sign = (c == '-') ? -1 : 1;
    int val = (c == '-') ? 0 : c - '0';
    c = getchar();
    while('0' <= c && c <= '9') {
        val = 10 * val + c - '0';
        c = getchar();
    }
    return val * sign;
}

void write(int x) {
    if(x < 10) {putchar(x + '0'); return;}
    write(x / 10);
    putchar(x % 10 + '0');
}

int addnode() {++ nt; return nt;}
void addnum(int x) {
    int r = root;
    REP(k, 31, 0) {
        int i = (x >> k) & 1;
        if(trie[r].child[i] == 0) {
            int l = addnode();
            trie[r].child[i] = l;
            trie[l].parent = r;
        }
        r = trie[r].child[i];
    }
    ++ trie[r].stop;
    ++ trie[r].cnt;
    while(r != root) {
        r = trie[r].parent;
        ++trie[r].cnt;
    }
}

void delnum(int x) {
    int r = root;
    REP(k, 31, 0) {
        int i = (x >> k) & 1;
        if(trie[r].child[i] == 0) return;
        r = trie[r].child[i];
    }
    if(trie[r].stop == 0) return;
    -- trie[r].stop;
    -- trie[r].cnt;
    while(r != root) {
        r = trie[r].parent;
        -- trie[r].cnt;
    }
}

int p_th(int p) {
    int r = root;
    int x = 0;
    REP(k, 31, 0) {
        int i = trie[r].child[0];
        if(p > trie[i].cnt) {
            x |= (1 << k);
            p -= trie[i].cnt;
            r = trie[r].child[1];
        } else r = trie[r].child[0];
    }
    return x;
}

int main() {
    //freopen("main.inp", "r", stdin);
    n = read(), m = read();
    FOR(i, 1, n) a[i] = read();
    root = addnode();
    FOR(i, 1, n) addnum(a[i]);
    FOR(i, 1, m) {
        int x;
        x = read();
        if(x > 0) addnum(x);
        else delnum(p_th(abs(x)));
    }
    if(p_th(1) > 0) {
        write(p_th(1));
        return 0;
    }
    puts("0");
}


#include <bits/stdc++.h>
using namespace std;

#define MAX 1000005
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define REP(i, a, b) for(int i = a; i >= b; i--)

struct Tnode {int stop, cnt, parent, child[2];};
Tnode trie[MAX];
int n, m, nt = 0, root = 0;
int a[MAX], b[MAX];

int addnode() {++ nt; return nt;}
void addnum(int x) {
    int r = root;
    REP(k, 31, 0) {
        int i = (x >> k) & 1;
        if(trie[r].child[i] == 0) {
            int l = addnode();
            trie[r].child[i] = l;
            trie[l].parent = r;
        }
        r = trie[r].child[i];
    }
    ++ trie[r].stop;
    ++ trie[r].cnt;
    while(r != root) {
        r = trie[r].parent;
        ++trie[r].cnt;
    }
}

void delnum(int x) {
    int r = root;
    REP(k, 31, 0) {
        int i = (x >> k) & 1;
        if(trie[r].child[i] == 0) return;
        r = trie[r].child[i];
    }
    if(trie[r].stop == 0) return;
    -- trie[r].stop;
    -- trie[r].cnt;
    while(r != root) {
        r = trie[r].parent;
        -- trie[r].cnt;
    }
}

int p_th(int p) {
    int r = root;
    int x = 0;
    REP(k, 31, 0) {
        int i = trie[r].child[0];
        if(p > trie[i].cnt) {
            x |= (1 << k);
            p -= trie[i].cnt;
            r = trie[r].child[1];
        } else r = trie[r].child[0];
    }
    return x;
}

int main() {
    //freopen("main.inp", "r", stdin);
    cin >> n >> m;
    FOR(i, 1, n) cin >> a[i];
    FOR(i, 1, m) cin >> b[i];
    root = addnode();
    FOR(i, 1, n) addnum(a[i]);
    FOR(i, 1, m) {
        if(b[i] > 0) addnum(p_th(b[i]));
        else delnum(p_th(abs(b[i])));
    }
    if(p_th(1) > 0) {
        cout << p_th(1);
        return 0;
    }
    cout << 0;
}


#include <bits/stdc++.h>
using namespace std;

#define MAX 1000005
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define REP(i, a, b) for(int i = a; i >= b; i--)

struct Tnode {int stop, cnt, parent, child[2];};
Tnode trie[MAX];
int n, m, nt = 0, root = 0;
int a[MAX], b[MAX];

int addnode() {++ nt; return nt;}
void addnum(int x) {
    int r = root;
    REP(k, 31, 0) {
        int i = (x >> k) & 1;
        if(trie[r].child[i] == 0) {
            int l = addnode();
            trie[r].child[i] = l;
            trie[l].parent = r;
        }
        r = trie[r].child[i];
    }
    ++ trie[r].stop;
    ++ trie[r].cnt;
    while(r != root) {
        r = trie[r].parent;
        ++trie[r].cnt;
    }
}

void delnum(int x) {
    int r = root;
    REP(k, 31, 0) {
        int i = (x >> k) & 1;
        if(trie[r].child[i] == 0) return;
        r = trie[r].child[i];
    }
    if(trie[r].stop == 0) return;
    -- trie[r].stop;
    -- trie[r].cnt;
    while(r != root) {
        r = trie[r].parent;
        -- trie[r].cnt;
    }
}

int p_th(int p) {
    int r = root;
    int x = 0;
    REP(k, 31, 0) {
        int i = trie[r].child[0];
        if(p > trie[i].cnt) {
            x |= (1 << k);
            p -= trie[i].cnt;
            r = trie[r].child[1];
        } else r = trie[r].child[0];
    }
    return x;
}

int main() {
    //freopen("main.inp", "r", stdin);
    scanf("%d %d", &n, &m);
    FOR(i, 1, n) scanf("%d", &a[i]);
    FOR(i, 1, m) scanf("%d", &b[i]);
    root = addnode();
    FOR(i, 1, n) addnum(a[i]);
    FOR(i, 1, m) {
        if(b[i] > 0) addnum(p_th(b[i]));
        else delnum(p_th(abs(b[i])));
    }
    if(p_th(1) > 0) {
        printf("%d", p_th(1));
        return 0;
    }
    printf("%d", 0);
}


#include<bits/stdc++.h>
using namespace std;
#define in long long int

struct FenwickTree {
    vector<in> bit;
    in n;

    FenwickTree(in n) {
        this->n = n;
        bit.assign(n, 0);
    }

    in sum(in r) {
        in ret = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret += bit[r];
        return ret;
    }

    in sum(in l, in r) {
        return sum(r) - sum(l - 1);
    }

    void add(in idx, in delta) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] += delta;
    }

    void rm(in x){
        in beg = 0, end = n;
        while(beg<end){
            in mid = beg + (end-beg)/2;
            if(sum(mid)<x) beg = mid+1;
            else end = mid;
        }
        add(beg, -1);
    }
};

const in N = 1e6+201;
FenwickTree fn = FenwickTree(N);

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    in n,q,x;
    cin>>n>>q;
    for(in i=0;i<n;i++){
        cin>>x;
        fn.add(x, 1);
    }
    for(in i=0;i<q;i++){
        cin>>x;
        if(x<0){
            fn.rm(abs(x));
        }
        else{
            fn.add(x, 1);
        }
    }
    in ans =0;
    in beg = 0, end = N;
    while(beg<end){
        in mid = beg + (end-beg)/2;
        if(fn.sum(mid)<1) beg = mid+1;
        else end = mid;
    }
    cout<<(beg==N?0:beg);

    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define in long long int

const in N = 1e6+201;
in bit[N];
in n;

in sum(in r) {
    in ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1)
        ret += bit[r];
    return ret;
}

in sum(in l, in r) {
    return sum(r) - sum(l - 1);
}

void add(in idx, in delta) {
    for (; idx < n; idx = idx | (idx + 1))
        bit[idx] += delta;
}

void rm(in x){
    in beg = 1, end = n;
    while(beg<end){
        in mid = beg + (end-beg)/2;
        if(sum(mid)<x) beg = mid+1;
        else end = mid;
    }
    add(beg, -1);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    memset(bit, 0, sizeof(bit));

    in q,x;
    cin>>n>>q;
    for(in i=0;i<n;i++){
        cin>>x;
        add(x, 1);
    }
    for(in i=0;i<q;i++){
        cin>>x;
        if(x<0){
            rm(abs(x));
        }
        else{
            add(x, 1);
        }
    }
    in ans =0;
    in beg = 1, end = n;
    while(beg<end){
        in mid = beg + (end-beg)/2;
        if(sum(mid)<1) beg = mid+1;
        else end = mid;
    }
    cout<<(beg==n?0:beg);

    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define in long long int

struct FenwickTree {
    vector<in> bit;
    in n;

    FenwickTree(in n) {
        this->n = n;
        bit.assign(n, 0);
    }

    FenwickTree(vector<in> a) : FenwickTree(a.size()) {
        for (in i = 0; i < a.size(); i++)
            add(i, a[i]);
    }

    in sum(in r) {
        in ret = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret += bit[r];
        return ret;
    }

    in sum(in l, in r) {
        return sum(r) - sum(l - 1);
    }

    void add(in idx, in delta) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] += delta;
    }

    void rm(in x){
        in beg = 0, end = n;
        while(beg<end){
            // cout<<beg<<" "<<end<<"\n";
            in mid = beg + (end-beg)/2;
            if(sum(mid)<x) beg = mid+1;
            else end = mid;
        }
        // cout<<end<<"\n";
        add(end, -1);
    }

    void print(){
        for(in i=1;i<10;i++){
            cout<<sum(i)<<" ";
        }
        cout<<"\n";
    }
};

FenwickTree fn = FenwickTree(1e6);

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    in n,q,x;
    cin>>n>>q;
    for(in i=0;i<n;i++){
        cin>>x;
        fn.add(x, 1);
    }
    // fn.print();
    for(in i=0;i<q;i++){
        cin>>x;
        if(x<0){
            fn.rm(abs(x));
        }
        else{
            fn.add(x, 1);
        }
    }
    in ans =0;
    for(in i=0;i<1e6;i++){
        if(fn.sum(i)!=0){
            ans = i;
            break;
        }
    }
    cout<<ans;

    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define in long long int

const in N = 1e6+201;
in bit[N];

in sum(in r) {
    in ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1)
        ret += bit[r];
    return ret;
}

in sum(in l, in r) {
    return sum(r) - sum(l - 1);
}

void add(in idx, in delta) {
    for (; idx < N; idx = idx | (idx + 1))
        bit[idx] += delta;
}

void rm(in x){
    in beg = 1, end = N;
    while(beg<end){
        in mid = beg + (end-beg)/2;
        if(sum(mid)<x) beg = mid+1;
        else end = mid;
    }
    // cout<<beg<<"\n";
    add(beg, -1);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    memset(bit, 0, sizeof(bit));

    in n,q,x;
    cin>>n>>q;
    for(in i=0;i<n;i++){
        cin>>x;
        add(x, 1);
    }
    for(in i=0;i<q;i++){
        cin>>x;
        if(x<0){
            rm(abs(x));
        }
        else{
            add(x, 1);
        }
    }

    for(in i=1;i<N;i++){
        if(sum(i)!=0){
            cout<<i;
            return 0;
        }
    }
    cout<<0;

    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define in long long int

const in N = 1e6+201;
in bit[N];
in n;

in sum(in r) {
    in ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1)
        ret += bit[r];
    return ret;
}

in sum(in l, in r) {
    return sum(r) - sum(l - 1);
}

void add(in idx, in delta) {
    for (; idx < n; idx = idx | (idx + 1))
        bit[idx] += delta;
}

void rm(in x){
    in beg = 1, end = n;
    while(beg<end){
        in mid = beg + (end-beg)/2;
        if(sum(mid)<x) beg = mid+1;
        else end = mid;
    }
    add(beg, -1);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    memset(bit, 0, sizeof(bit));

    in q,x;
    cin>>n>>q;
    for(in i=0;i<n;i++){
        cin>>x;
        add(x, 1);
    }
    for(in i=0;i<q;i++){
        cin>>x;
        if(x<0){
            rm(abs(x));
        }
        else{
            add(x, 1);
        }
    }

    for(in i=1;i<=n;i++){
        if(sum(i)!=0){
            cout<<i;
            return 0;
        }
    }
    cout<<0;

    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define in long long int

const in N = 1e6+201;

struct FenwickTree {
    in bit[N];
    in n;

    FenwickTree(in n) {
        this->n = n;
        memset(bit, 0, sizeof(bit));
    }

    in sum(in r) {
        in ret = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret += bit[r];
        return ret;
    }

    in sum(in l, in r) {
        return sum(r) - sum(l - 1);
    }

    void add(in idx, in delta) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] += delta;
    }

    void rm(in x){
        in beg = 1, end = n;
        while(beg<end){
            in mid = beg + (end-beg)/2;
            if(sum(mid)<x) beg = mid+1;
            else end = mid;
        }
        add(beg, -1);
    }
};

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    in n,q,x;
    cin>>n>>q;
    FenwickTree fn = FenwickTree(n);
    for(in i=0;i<n;i++){
        cin>>x;
        fn.add(x, 1);
    }
    for(in i=0;i<q;i++){
        cin>>x;
        if(x<0){
            fn.rm(abs(x));
        }
        else{
            fn.add(x, 1);
        }
    }
    in ans =0;
    in beg = 1, end = n;
    while(beg<end){
        in mid = beg + (end-beg)/2;
        if(fn.sum(mid)<1) beg = mid+1;
        else end = mid;
    }
    cout<<(beg==n?0:beg);

    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define in long long int
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    in n,q,x;
    cin>>n>>q;
    map<in,in> v;
    for(in i=0;i<n;i++){
        cin>>x;
        v[x]+=1;
    }
    for(in i=0;i<q;i++){
        cin>>x;
        if(x<0){
            x = abs(x);
            in idx = 0;
            in val = -1;
            for(auto j:v){
                idx+=j.second;
                if(idx>=x){
                    v[j.first]-=1;
                    break;
                }
            }
        }
        else{
            v[x]+=1;
        }
    }
    in ans = 0;
    // for(auto i:v)cout<<i.first<<" ";
    // cout<<"\n";
    for(auto i:v){
        if(i.second!=0){
            ans = i.first;
            break;
        }
    }
    cout<<ans;

    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define in long long int
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    in n,q,x;
    cin>>n>>q;
    map<in,in> v;
    for(in i=0;i<n;i++){
        cin>>x;
        v[x]+=1;
    }
    for(in i=0;i<q;i++){
        cin>>x;
        if(x<0){
            x = abs(x);
            in idx = 0;
            in val = -1;
            for(auto j:v){
                idx++;
                if(idx==x){
                    v[j.first]-=1;
                    if(v[j.first]==0){
                        v.erase(j.first);
                    }
                    break;
                }
            }
        }
        else{
            v[x]+=1;
        }
    }
    in ans = 0;
    for(auto i:v){
        if(i.second!=0){
            ans = i.first;
            break;
        }
    }
    cout<<ans;

    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define in long long int

struct FenwickTree {
    vector<in> bit;
    in n;

    FenwickTree(in n) {
        this->n = n;
        bit.assign(n, 0);
    }

    in sum(in r) {
        in ret = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret += bit[r];
        return ret;
    }

    in sum(in l, in r) {
        return sum(r) - sum(l - 1);
    }

    void add(in idx, in delta) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] += delta;
    }

    void rm(in x){
        in beg = 0, end = n;
        while(beg<end){
            in mid = beg + (end-beg)/2;
            if(sum(mid)<x) beg = mid+1;
            else end = mid;
        }
        add(beg, -1);
    }
};

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    in n,q,x;
    cin>>n>>q;
    FenwickTree fn = FenwickTree(n);
    for(in i=0;i<n;i++){
        cin>>x;
        fn.add(x, 1);
    }
    for(in i=0;i<q;i++){
        cin>>x;
        if(x<0){
            fn.rm(abs(x));
        }
        else{
            fn.add(x, 1);
        }
    }
    in ans =0;
    in beg = 0, end = n;
    while(beg<end){
        in mid = beg + (end-beg)/2;
        if(fn.sum(mid)<1) beg = mid+1;
        else end = mid;
    }
    cout<<(beg==n?0:beg);

    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define in long long int
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    in n,q,x;
    cin>>n>>q;
    map<in,in> v;
    for(in i=0;i<n;i++){
        cin>>x;
        v[x]+=1;
    }
    for(in i=0;i<q;i++){
        cin>>x;
        if(x<0){
            x = abs(x);
            in idx = 0;
            in val = -1;
            for(auto j:v){
                idx+=j.second;
                if(idx>=x){
                    v[j.first]-=1;
                    if(v[j.first]==0){
                        v.erase(j.first);
                    }
                    break;
                }
            }
        }
        else{
            v[x]+=1;
        }
    }
    in ans = 0;
    // for(auto i:v)cout<<i.first<<" ";
    // cout<<"\n";
    for(auto i:v){
        if(i.second!=0){
            ans = i.first;
            break;
        }
    }
    cout<<ans;

    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define in long long int

const in N = 1e6+1;
in bit[N];

in sum(in r) {
    in ret = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1)
        ret += bit[r];
    return ret;
}

in sum(in l, in r) {
    return sum(r) - sum(l - 1);
}

void add(in idx, in delta) {
    for (; idx < N; idx = idx | (idx + 1))
        bit[idx] += delta;
}

void rm(in x){
    in beg = 1, end = N;
    while(beg<end){
        in mid = beg + (end-beg)/2;
        if(sum(mid)<x) beg = mid+1;
        else end = mid;
    }
    // cout<<beg<<"\n";
    add(beg, -1);
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    memset(bit, 0, sizeof(bit));

    in n,q,x;
    cin>>n>>q;
    for(in i=0;i<n;i++){
        cin>>x;
        add(x, 1);
    }
    for(in i=0;i<q;i++){
        cin>>x;
        if(x<0){
            rm(abs(x));
        }
        else{
            add(x, 1);
        }
    }

    for(in i=1;i<N;i++){
        if(sum(i)!=0){
            cout<<i;
            return 0;
        }
    }
    cout<<0;

    return 0 ;
}


#include<bits/stdc++.h>
using namespace std;
#define in long long int

struct FenwickTree {
    vector<in> bit;
    in n;

    FenwickTree(in n) {
        this->n = n;
        bit.assign(n, 0);
    }

    FenwickTree(vector<in> a) : FenwickTree(a.size()) {
        for (in i = 0; i < a.size(); i++)
            add(i, a[i]);
    }

    in sum(in r) {
        in ret = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1)
            ret += bit[r];
        return ret;
    }

    in sum(in l, in r) {
        return sum(r) - sum(l - 1);
    }

    void add(in idx, in delta) {
        for (; idx < n; idx = idx | (idx + 1))
            bit[idx] += delta;
    }

    void rm(in x){
        in beg = 0, end = n;
        while(beg<end){
            in mid = beg + (end-beg)/2;
            if(sum(mid)<x) beg = mid+1;
            else end = mid;
        }
        add(beg, -1);
    }
};

const in N = 1e6+201;
FenwickTree fn = FenwickTree(N);

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    in n,q,x;
    cin>>n>>q;
    for(in i=0;i<n;i++){
        cin>>x;
        fn.add(x, 1);
    }
    for(in i=0;i<q;i++){
        cin>>x;
        if(x<0){
            fn.rm(abs(x));
        }
        else{
            fn.add(x, 1);
        }
    }
    in ans =0;
    in beg = 0, end = N;
    while(beg<end){
        in mid = beg + (end-beg)/2;
        if(fn.sum(mid)<1) beg = mid+1;
        else end = mid;
    }
    cout<<(beg==N?0:beg);

    return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define forn(i, a, b)   for(int i=a; i<=b; ++i)
const int N = 2e5 +10;
int n;
int a[N], high[N];
int par[N][20], gcd[N][20];
vector<int> adj[N];
void dfs(int u, int prv = 0) {
    for(int i=0; i<adj[u].size(); ++i) {
        int v =adj[u][i];
        if (v == prv) continue;
        high[v] = high[u] +1;
        par[v][1] = u;
        gcd[v][1] = __gcd(a[v], a[u]);
        dfs(v, u);
    }
}
bool check(int mid) {
    for(int u=1; u<=n; ++u) if (high[u] >= mid)
    {
        int k = high[u], mx =0, ss = u;
        for(int i=17; i>= 0; --i) if (k>= (1<<i)) {
            mx = __gcd(mx, gcd[ss][i]);
            ss = par[par[ss][i]][1];
            k -= (1 << i);
        }
        if (mx > 1) return true;
    }
    return false;
}
main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n;
    for(int i=1; i<=n; ++i) cin >> a[i];
    for(int i=1; i <n; ++i) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    high[1] = 1;
    for(int i=1; i<=n; ++i) par[i][0] = i, gcd[i][0] = a[i];
    dfs(1);
    high[0] = -1;
    for(int i=2; i<18; ++i) {
        for(int u=1; u<=n; ++u) {
            par[u][i] = par[par[par[u][i-1]][1]][i-1];
            gcd[u][i] = __gcd(gcd[u][i-1], gcd[par[par[u][i-1]][1]][i-1]);
        }
    }
    int lo=0, hi=n, ans=0;
    while (lo <= hi) {
        int mid= lo+hi >> 1;
        if (check(mid)) {
            ans = mid;
            lo = mid+1;
        } else hi=mid-1;
    }
    cout << ans;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define forn(i, a, b)   for(int i=a; i<=b; ++i)
const int N = 2e5 +10;
int n, mx;
int a[N], high[N], prime[N];
bool over[N];
vector<int> adj[N];
vector<pair<int,int>> s[N];
void dfs(int u, int prv) {
    over[u] = true;
    for(int i=0; i<adj[u].size(); ++i)
    {
        int v = adj[u][i];
        if (v == prv) continue;
                high[v] = high[u] +1;
        dfs(v, u);
    }
    if (high[u] > high[mx]) mx = u;
}
int process(vector<pair<int, int>> s)
{
    vector<int> ver;
    for(int i=0; i<s.size(); ++i) {
        int u=s[i].first, v = s[i].second;
        adj[u].push_back(v);
        adj[v].push_back(u);
        ver.push_back(v);
        ver.push_back(u);
    }
    int ans = 0;
    for(int i=0; i<ver.size(); ++i) if (!over[ver[i]])
    {
        int u = ver[i];
        mx = u;
        high[u] = 1;
        dfs(u, u);
        high[mx] = 1;
        dfs(mx, mx);
        ans = max(ans, high[mx]);
    }
    for(int i=0; i<ver.size(); ++i) {
        int u = ver[i];
        adj[u].clear();
        over[u] = false;
        high[u] = 0;
    }
    return ans;
}
main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n;
    int ans = 0;
    for(int i=1; i<=n; ++i) {
        cin >> a[i];
        if (a[i] > 1) ans = 1;
    }
    vector<int> vv;
    for(int i=2; i<= 2*1e5; ++i) if (!prime[i]) {
        vv.push_back(i);
        for(int j =2*i; j<=2*1e5; j+=i) prime[j] = true;
    }
    for(int i=1; i <n; ++i) {
        int u, v; cin >> u >> v;
        int x = __gcd(a[v], a[u]);
        int m = sqrt(x);
        for(int k=0; k<vv.size() && vv[k] <= m; ++k) {
            if (x == 1) break;
            if (x % vv[k] == 0) {
                s[vv[k]].push_back({v, u});
                while (x % vv[k] == 0) x /= vv[k];
            }
        }
        if (x > 1) s[x].push_back({v, u});
    }
    for(int i=0; i<vv.size(); ++i) if (ans < s[vv[i]].size() + 1)
    {
        ans = max(ans, process(s[vv[i]]));
    }
    cout << ans;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
using namespace std;
const int N = 2e5 + 300;
const ll Mod = 998244353, OO = 2e9;
int n;
int ans = 0;
int A[N], prime[N];
int mx[N][50][4];
vector<int> open[N] , g[N];
void Sieve() {
	memset(prime, true, sizeof(prime));
	for (int i = 2; i < N; ++i) {
		if (prime[i]) {
			open[i].pb(i);
			for (int j = i + i; j < N; j += i) {
				open[j].pb(i);
				prime[j] = false;
			}
		}
	}
}
bool ok = false;
void update(int src, int ch) {
	for (int i = 0; i < open[A[src]].size(); ++i) {
		sort(mx[src][i], mx[src][i] + 3);
	}
	for (int i = 0; i < open[A[ch]].size(); ++i) {
		sort(mx[src][i], mx[src][i] + 3);
	}
	for (int i = 0; i < open[A[src]].size(); ++i) {
		for (int j = 0; j < open[A[ch]].size(); ++j) {
			if (open[A[src]][i] == open[A[ch]][j]) {
				mx[src][i][0] = max(mx[src][i][0], mx[ch][j][2] + 1);
			}
		}
	}
	for (int i = 0; i < open[A[src]].size(); ++i) {
		sort(mx[src][i], mx[src][i] + 3);
	}
}
void dfs(int src, int par) {
	for (int i = 0; i < open[A[src]].size(); ++i) {
		for (int j = 0; j < 3; ++j) {
			mx[src][i][j] = 0;
		}
	}
	for (int i = 0; i < g[src].size(); ++i) {
		if (g[src][i] == par) {
			continue;
		}
		dfs(g[src][i], src);
		update(src, g[src][i]);
	}

	for (int i = 0; i < open[A[src]].size(); ++i) {
		ans = max(ans, mx[src][i][2] + mx[src][i][1] + 1);
	}
}
int main() {

	cin >> n;
	Sieve();
	for (int i = 1; i <= n; ++i) {
		cin >> A[i];
		ok |= (A[i] > 1);
	}

	for (int i = 1; i < n; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	dfs(1, -1);
	if (!ok) ans = 0;
	cout << ans << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
using namespace std;
const int N = 2e5 + 300;
const ll Mod = 998244353, OO = 2e9;
int n;
int ans = 0;
int A[N], mx1[N][100], mx2[N][100] , prime[N];
vector<int> open[N] , g[N];
void Sieve() {
	memset(prime, true, sizeof(prime));
	for (int i = 2; i < N; ++i) {
		if (prime[i]) {
			open[i].pb(i);
			for (int j = i + i; j < N; j += i) {
				open[j].pb(i);
				prime[j] = false;
			}
		}
	}
}
void update(int src, int ch) {
	for (int i = 0; i < open[A[src]].size(); ++i) {
		for (int j = 0; j < open[A[ch]].size(); ++j) {
			if (open[A[src]][i] == open[A[ch]][j]) {
				mx2[src][i] = max(mx2[src][i], mx1[ch][j] + 1);
				if (mx2[src][i] > mx1[src][i]) swap(mx2[src][i], mx1[src][i]);
				break;
			}
		}
	}
}
void dfs(int src, int par) {
	for (int i = 0; i < open[A[src]].size(); ++i) {
		mx1[src][i] = 1;
		mx2[src][i] = 1;
	}
	for (int i = 0; i < g[src].size(); ++i) {
		if (g[src][i] == par)
			continue;
		dfs(g[src][i], src);
		update(src, g[src][i]);
	}
	for (int i = 0; i < open[A[src]].size(); ++i) {
		ans = max(ans, mx2[src][i] + mx1[src][i] - 1);
	}
}
int main() {

	cin >> n;
	Sieve();
	for (int i = 1; i <= n; ++i)
		cin >> A[i];
	for (int i = 1; i < n; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	dfs(1, -1);
	cout << ans << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
using namespace std;
const int N = 2e5 + 300;
const ll Mod = 998244353, OO = 2e9;
int n;
int ans = 0;
int A[N], mx1[N][100], mx2[N][100] , prime[N];
vector<int> open[N] , g[N];
void Sieve() {
	memset(prime, true, sizeof(prime));
	for (int i = 2; i < N; ++i) {
		if (prime[i]) {
			open[i].pb(i);
			for (int j = i + i; j < N; j += i) {
				open[j].pb(i);
				prime[j] = false;
			}
		}
	}
}
bool ok = false;
void update(int src, int ch) {
	sort(mx1[src], mx1[src] + 3);
	for (int i = 0; i < open[A[src]].size(); ++i) {
		for (int j = 0; j < open[A[ch]].size(); ++j) {
			if (open[A[src]][i] == open[A[ch]][j]) {
				mx1[src][0] = max(mx1[src][0], mx1[ch][2] + 1);

			}
		}
	}
	sort(mx1[src], mx1[src] + 3);
}
void dfs(int src, int par) {
	for (int i = 0; i < 3; ++i) {
		mx1[src][i] = 0;
	}
	for (int i = 0; i < g[src].size(); ++i) {
		if (g[src][i] == par)
			continue;
		dfs(g[src][i], src);
		update(src, g[src][i]);
	}
	ans = max(ans, mx1[src][1] + mx1[src][2] + 1);
}
int main() {

	cin >> n;
	Sieve();
	for (int i = 1; i <= n; ++i) {
		cin >> A[i];
		ok |= (A[i] > 1);
	}

	for (int i = 1; i < n; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	dfs(1, -1);
	if (!ok) ans = 0;
	cout << ans << endl;
}


#include<bits/stdc++.h>
using  namespace std;
#define ll long long
#define pb push_back
#define sc second
#define fr first
#define mp make_pair
#define eb emplace_back
const int MOD = 998244353, OO = 1e9;
const int N = 2e5 + 3;
int n;
vector<int>g[N];
int prime[N] , a[N] , mx[N][8][3];
vector<int> opened[N];
int gcd(int a, int b) {
	if (b == 0)
		return a;
	return gcd(b, a%b);
}
void sieve() {
	memset(prime, 1, sizeof(prime));
	for (int i = 2; i*i <= N; ++i) {
		if (!prime[i])
			continue;
		opened[i].pb(i);
		for (int j = i + i; j <= N; j += i) {
			prime[j] = false;
			opened[j].pb(i);
		}
	}
}
void update(int first, int second) {
	for (int i = 0; i < opened[a[first]].size(); ++i) {
		sort(mx[first][i], mx[first][i] + 3, greater<int>());
	}
	for (int i = 0; i < opened[a[second]].size(); ++i) {
		sort(mx[second][i], mx[second][i] + 3, greater<int>());
	}
	for (int i = 0; i < opened[a[first]].size(); ++i) {
		for (int j = 0; j < opened[a[second]].size(); ++j) {
			int aa = opened[a[first]][i];
			int bb = opened[a[second]][j];
			if (aa == bb) {
				mx[first][i][2] = mx[second][j][0] + 1;
				break;
			}
		}
	}
	for (int i = 0; i < opened[a[first]].size(); ++i) {
		sort(mx[first][i], mx[first][i] + 3,greater<int>());
	}
}
int ans = 0;
void dfs(int src, int par) {
	for (int i = 0; i < g[src].size(); ++i) {
		int nx = g[src][i];
		if (par == nx)
			continue;
		dfs(nx, src);
		update(src, nx);
	}
	for (int i = 0; i < opened[a[src]].size(); ++i) {
		ans = max(ans, mx[src][i][0] + mx[src][i][1] + 1);
	}
}
int ok = 0;
int main() {
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		ok |= (a[i] > 1);
	}
	sieve();
	int mx = 0;
	for (int i = 1; i < n; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	dfs(1, -1);
	if (!ok)ans = 0;
	cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
using namespace std;
const int N = 2e5 + 300;
const ll Mod = 998244353, OO = 2e9;
int n;
int ans = 0;
int A[N], mx1[N][100], mx2[N][100] , prime[N];
vector<int> open[N] , g[N];
void Sieve() {
	memset(prime, true, sizeof(prime));
	for (int i = 2; i < N; ++i) {
		if (prime[i]) {
			open[i].pb(i);
			for (int j = i + i; j < N; j += i) {
				open[j].pb(i);
				prime[j] = false;
			}
		}
	}
}
void update(int src, int ch) {
	for (int i = 0; i < open[A[src]].size(); ++i) {
		for (int j = 0; j < open[A[ch]].size(); ++j) {
			if (open[A[src]][i] == open[A[ch]][j]) {
				mx2[src][i] = max(mx2[src][i], mx1[ch][j] + 1);
				if (mx2[src][i] > mx1[src][i]) swap(mx2[src][i], mx1[src][i]);
			}
		}
	}
}
void dfs(int src, int par) {
	for (int i = 0; i < open[A[src]].size(); ++i) {
		mx1[src][i] = 1;
		mx2[src][i] = 1;
	}
	for (int i = 0; i < g[src].size(); ++i) {
		if (g[src][i] == par)
			continue;
		dfs(g[src][i], src);
		update(src, g[src][i]);
	}
	for (int i = 0; i < open[A[src]].size(); ++i) {
		ans = max(ans, mx2[src][i] + mx1[src][i] - 1);
	}
}
int main() {

	cin >> n;
	Sieve();
	for (int i = 1; i <= n; ++i)
		cin >> A[i];
	for (int i = 1; i < n; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	dfs(1, -1);
	cout << ans << endl;
}


#include<bits/stdc++.h>
using  namespace std;
#define ll long long
#define pb push_back
#define sc second
#define fr first
#define mp make_pair
#define eb emplace_back
const int MOD = 998244353, OO = 1e9;
const int N = 1e5 + 3;
int n;
vector<int>g[N + 1];
int prime[N + 1] , a[N + 1] , mx[N + 1][8][3];
vector<int> opened[N + 1];
int gcd(int a, int b) {
	if (b == 0)
		return a;
	return gcd(b, a%b);
}
void sieve() {
	memset(prime, 1, sizeof(prime));
	for (int i = 2; i*i <= N; ++i) {
		if (!prime[i])
			continue;
		opened[i].pb(i);
		for (int j = i + i; j <= N; j += i) {
			prime[j] = false;
			opened[j].pb(i);
		}
	}
}
void update(int first, int second) {
	for (int i = 0; i < opened[a[first]].size(); ++i) {
		sort(mx[first][i], mx[first][i] + 3, greater<int>());
	}
	for (int i = 0; i < opened[a[second]].size(); ++i) {
		sort(mx[second][i], mx[second][i] + 3, greater<int>());
	}
	for (int i = 0; i < opened[a[first]].size(); ++i) {
		for (int j = 0; j < opened[a[second]].size(); ++j) {
			int aa = opened[a[first]][i];
			int bb = opened[a[second]][j];
			if (aa == bb) {
				mx[first][i][2] = mx[second][j][0] + 1;
				break;
			}
		}
	}
	for (int i = 0; i < opened[a[first]].size(); ++i) {
		sort(mx[first][i], mx[first][i] + 3,greater<int>());
	}
}
int ans = 0;
void dfs(int src, int par) {
	for (int i = 0; i < g[src].size(); ++i) {
		int nx = g[src][i];
		if (par == nx)
			continue;
		dfs(nx, src);
		update(src, nx);
	}
	for (int i = 0; i < opened[a[src]].size(); ++i) {
		ans = max(ans, mx[src][i][0] + mx[src][i][1] + 1);
	}
}
int ok = 0;
int main() {
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		ok |= (a[i] > 1);
	}
	sieve();
	int mx = 0;
	for (int i = 1; i < n; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	dfs(1, -1);
	cout << ans;
}


#include<bits/stdc++.h>
using  namespace std;
#define ll long long
#define pb push_back
#define sc second
#define fr first
#define mp make_pair
#define eb emplace_back
const int MOD = 998244353, OO = 1e9;
const int N = 2e5 + 3;
int n;
vector<int>g[N];
int prime[N] , a[N] , mx[N][8][3];
vector<int> opened[N];
int gcd(int a, int b) {
	if (b == 0)
		return a;
	return gcd(b, a%b);
}
void sieve() {
	memset(prime, 1, sizeof(prime));
	for (int i = 2; i*i <= N; ++i) {
		if (!prime[i])
			continue;
		opened[i].pb(i);
		for (int j = i + i; j <= N; j += i) {
			prime[j] = false;
			opened[j].pb(i);
		}
	}
}
void update(int first, int second) {
	for (int i = 0; i < opened[a[first]].size(); ++i) {
		sort(mx[first][i], mx[first][i] + 3, greater<int>());
	}
	for (int i = 0; i < opened[a[second]].size(); ++i) {
		sort(mx[second][i], mx[second][i] + 3, greater<int>());
	}
	for (int i = 0; i < opened[a[first]].size(); ++i) {
		for (int j = 0; j < opened[a[second]].size(); ++j) {
			int aa = opened[a[first]][i];
			int bb = opened[a[second]][j];
			if (aa == bb) {
				mx[first][i][2] =max( mx[second][j][0] + 1,mx[first][i][2]);

			}
		}
	}
	for (int i = 0; i < opened[a[first]].size(); ++i) {
		sort(mx[first][i], mx[first][i] + 3,greater<int>());
	}
}
int ans = 0;
void dfs(int src, int par) {
	for (int i = 0; i < g[src].size(); ++i) {
		int nx = g[src][i];
		if (par == nx)
			continue;
		dfs(nx, src);
		update(src, nx);
	}
	for (int i = 0; i < opened[a[src]].size(); ++i) {
		ans = max(ans, mx[src][i][0] + mx[src][i][1] + 1);
	}
}
int ok = 0;
int main() {
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		ok |= (a[i] > 1);
	}
	sieve();
	for (int i = 1; i < n; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	dfs(1, -1);
	if (!ok)ans = 0;
	cout << ans;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
using namespace std;
const int N = 2e5 + 300;
const ll Mod = 998244353, OO = 2e9;
int n;
int ans = 0;
int A[N], mx1[N][100], mx2[N][100] , prime[N];
vector<int> open[N] , g[N];
void Sieve() {
	memset(prime, true, sizeof(prime));
	for (int i = 2; i < N; ++i) {
		if (prime[i]) {
			open[i].pb(i);
			for (int j = i + i; j < N; j += i) {
				open[j].pb(i);
				prime[j] = false;
			}
		}
	}
}
bool ok = false;
void update(int src, int ch) {
	sort(mx1[src], mx1[src] + 3);
	sort(mx1[ch], mx1[ch] + 3);
	for (int i = 0; i < open[A[src]].size(); ++i) {
		for (int j = 0; j < open[A[ch]].size(); ++j) {
			if (open[A[src]][i] == open[A[ch]][j]) {
				mx1[src][0] = max(mx1[src][0], mx1[ch][2] + 1);

			}
		}
	}
	sort(mx1[src], mx1[src] + 3);
}
void dfs(int src, int par) {
	for (int i = 0; i < 3; ++i) {
		mx1[src][i] = 0;
	}
	for (int i = 0; i < g[src].size(); ++i) {
		if (g[src][i] == par)
			continue;
		dfs(g[src][i], src);
		update(src, g[src][i]);
	}
	ans = max(ans, mx1[src][1] + mx1[src][2] + 1);
}
int main() {

	cin >> n;
	Sieve();
	for (int i = 1; i <= n; ++i) {
		cin >> A[i];
		ok |= (A[i] > 1);
	}

	for (int i = 1; i < n; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	dfs(1, -1);
	if (!ok) ans = 0;
	cout << ans << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
using namespace std;
const int N = 2e5 + 300;
const ll Mod = 998244353, OO = 2e9;
int n;
int ans = 0;
int A[N], mx1[N][100], mx2[N][100] , prime[N];
int mx[N][100][4];
vector<int> open[N] , g[N];
void Sieve() {
	memset(prime, true, sizeof(prime));
	for (int i = 2; i < N; ++i) {
		if (prime[i]) {
			open[i].pb(i);
			for (int j = i + i; j < N; j += i) {
				open[j].pb(i);
				prime[j] = false;
			}
		}
	}
}
bool ok = false;
void update(int src, int ch) {
	for (int i = 0; i < open[A[src]].size(); ++i) {
		sort(mx[src][i], mx[src][i] + 3);
	}
	for (int i = 0; i < open[A[ch]].size(); ++i) {
		sort(mx[src][i], mx[src][i] + 3);
	}
	for (int i = 0; i < open[A[src]].size(); ++i) {
		for (int j = 0; j < open[A[ch]].size(); ++j) {
			if (open[A[src]][i] == open[A[ch]][j]) {
				mx[src][i][0] = max(mx[src][i][0], mx[ch][j][2] + 1);
			}
		}
	}
	for (int i = 0; i < open[A[src]].size(); ++i) {
		sort(mx[src][i], mx[src][i] + 3);
	}
}
void dfs(int src, int par) {
	for (int i = 0; i < open[A[src]].size(); ++i) {
		for (int j = 0; j < 3; ++j) {
			mx[src][i][j] = 0;
		}
	}
	for (int i = 0; i < g[src].size(); ++i) {
		if (g[src][i] == par) {
			continue;
		}
		dfs(g[src][i], src);
		update(src, g[src][i]);
	}

	for (int i = 0; i < open[A[src]].size(); ++i) {
		ans = max(ans, mx[src][i][2] + mx[src][i][1] + 1);
	}
}
int main() {

	cin >> n;
	Sieve();
	for (int i = 1; i <= n; ++i) {
		cin >> A[i];
		ok |= (A[i] > 1);
	}

	for (int i = 1; i < n; ++i) {
		int a, b;
		cin >> a >> b;
		g[a].pb(b);
		g[b].pb(a);
	}
	dfs(1, -1);
	if (!ok) ans = 0;
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pll pair <ll,ll>
#define nl "\n"
vector <ll> adj[300005];
ll maxi,val[300005],vis[300005],ans;
vector <ll> node;
ll cur_glob;
void dfs(ll x,ll p,ll dep,ll curr)
{
	vis[x]=1;
	if(dep>maxi)
	{
		maxi=dep;
		node.clear();
		if(x!=cur_glob)
		node.pb(x);
	}
	else if(dep==maxi)
	{
		maxi=dep;
		if(x!=cur_glob)
		node.pb(x);
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(__gcd(curr,val[it])!=1)
			{
				dfs(it,x,dep+1,__gcd(curr,val[it]));
			}
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	ll n,i;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>val[i];
	}
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=1;i<=n;i++)
	{
		if(!vis[i])
		{
			maxi=0;
			cur_glob=i;
			dfs(i,-1,0,val[i]);
			if(node.empty())
			{
				if(val[i]!=1)
					ans=max(ans,1ll);
			}
			for(ll j=0;j<node.size();j++)
			{
				maxi=0;
				dfs(node[j],-1,0,val[node[j]]);
				if(node[j]==i && val[i]==1)
					continue;
				else
					ans=max(ans,maxi+1);
			}
		}
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define int long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(a[it]%pri==0)
			{
				dfs(it,x,dep+1,pri);
			}
		}
	}
}
void factorise(int n,int x)
{
	for(int i=0;i<primes.size();i++)
	{
		if(n==0)
			break;
		if(n%primes[i]==0)
		{
			fact[primes[i]].pb(x);
		}
		while(n%primes[i]==0)
		{
			n=n/primes[i];
		}
	}
}
void soe()
{
	int prime[L]={0};
	int i,j;
	for(int i=2;i<L/1000;i++)
	{
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
				prime[j]=1;
		}
	}
	primes.pb(199999);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
		{
			vis[it]=0;
		}
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define nl "\n"
const int L = 200001;
vector <int> adj[L],fact[L],primes,now;
int maxi,node,ans,vis[L],a[L],spf[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
		maxi=dep,node=x;
	for(auto it:adj[x])
		if(it!=p)
			if(a[it]%pri==0)
				dfs(it,x,dep+1,pri);
}
void factorise(int n,int x)
{
	while(n>1)
	{
		fact[spf[n]].pb(x);
		int ux=spf[n];
		while(n%ux==0)
			n=n/ux;
	}
}
void soe()
{
	int prime[L]={0};
	for(int i=1;i<L;i++)
		spf[i]=i;
	int i,j;
	for(int i=2;i<L;i++)
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
			{
				prime[j]=1;
				if(spf[j]==j)
					spf[j]=i;
			}
		}
}
int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
			vis[it]=0;
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pll pair <ll,ll>
#define nl "\n"
vector <ll> adj[300005];
ll maxi=-1,node,val[300005],vis[300005],ans;
void dfs(ll x,ll p,ll dep,ll curr)
{
	vis[x]=1;
	if(dep>=maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			// if(it==2)
			// {
			// 	cout<<curr<<nl;
			// }
			if(__gcd(curr,val[it])!=1)
			{
				dfs(it,x,dep+1,__gcd(curr,val[it]));
			}
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	ll n,i;
	cin>>n;
	for(i=0;i<n;i++)
		cin>>val[i+1];
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=1;i<=1;i++)
	{
		maxi=0;
		if(!vis[i])
		{
			dfs(i,-1,0,val[i]);
			// cout<<i<<" "<<node<<" "<<maxi<<nl;
			maxi=0;
			dfs(node,-1,0,val[node]);
			// cout<<i<<" "<<node<<" "<<maxi<<nl;
			if(node==i && val[i]==1)
				continue;
			else
				ans=max(ans,maxi+1);
		}
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pll pair <ll,ll>
#define nl "\n"
vector <ll> adj[300005];
ll maxi,val[300005],vis[300005],ans;
vector <ll> node;
ll cur_glob;
void dfs(ll x,ll p,ll dep,ll curr)
{
	vis[x]=1;
	if(dep>maxi)
	{
		maxi=dep;
		node.clear();
		if(x!=cur_glob)
		node.pb(x);
	}
	else if(dep==maxi)
	{
		maxi=dep;
		if(x!=cur_glob)
		node.pb(x);
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(__gcd(curr,val[it])!=1)
			{
				dfs(it,x,dep+1,__gcd(curr,val[it]));
			}
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	ll n,i;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>val[i];
	}
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=1;i<=n;i++)
	{
		if(!vis[i])
		{
			maxi=0;
			cur_glob=i;
			dfs(i,-1,0,val[i]);
			if(node.empty())
			{
				if(val[i]!=1)
					ans=max(ans,1ll);
			}
			for(ll j=0;j<node.size();j++)
			{
				cout<<node[j]<<nl;
				maxi=0;
				dfs(node[j],-1,0,val[node[j]]);
				if(node[j]==i && val[i]==1)
					continue;
				else
					ans=max(ans,maxi+1);
			}
		}
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L],spf[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
		maxi=dep,node=x;
	for(auto it:adj[x])
		if(it!=p)
			if(a[it]%pri==0)
				dfs(it,x,dep+1,pri);
}
void factorise(int n,int x)
{
	while(n>1)
	{
		fact[spf[n]].pb(x);
		int ux=spf[n];
		while(n%ux==0)
			n=n/ux;
		// n=spf[n];
	}
}
void soe()
{
	int prime[L]={0};
	for(int i=1;i<L;i++)
		spf[i]=i;
	int i,j;
	for(int i=2;i<L;i++)
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
			{
				prime[j]=1;
				if(spf[j]==j)
					spf[j]=i;
			}
		}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
			vis[it]=0;
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pll pair <ll,ll>
#define nl "\n"
const ll L = 200005;
vector <ll> primes;
vector <ll> now;
vector <ll> adj[L],fact[L];
ll maxi,node,ans,vis[L],a[L];
void dfs(ll x,ll p,ll dep,ll pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(a[it]%pri==0)
			{
				dfs(it,x,dep+1,pri);
			}
		}
	}
}
void factorise(ll n,ll x)
{
	for(ll i=0;i<primes.size();i++)
	{
		if(n==0)
			break;
		if(n%primes[i]==0)
			fact[primes[i]].pb(x);
		while(n%primes[i]==0)
		{
			n=n/primes[i];
		}
	}
}
void soe()
{
	ll prime[L]={0};
	ll i,j;
	for(ll i=2;i<L;i++)
	{
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
				prime[j]=1;
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	ll n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
		{
			vis[it]=0;
		}
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define int long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(a[it]%pri==0)
			{
				dfs(it,x,dep+1,pri);
			}
		}
	}
}
void factorise(int n,int x)
{
	for(int i=0;i<primes.size();i++)
	{
		if(n==0)
			break;
		if(n%primes[i]==0)
		{
			fact[primes[i]].pb(x);
		}
		while(n%primes[i]==0)
		{
			n=n/primes[i];
		}
	}
}
void soe()
{
	int prime[L]={0};
	int i,j;
	for(int i=2;i<L/100;i++)
	{
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
				prime[j]=1;
		}
	}
	primes.pb(199999);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
		{
			vis[it]=0;
		}
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define int long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(a[it]%pri==0)
			{
				dfs(it,x,dep+1,pri);
			}
		}
	}
}
void factorise(int n,int x)
{
	for(int i=0;i<primes.size();i++)
	{
		if(n==0)
			break;
		if(n%primes[i]==0)
		{
			fact[primes[i]].pb(x);
		}
		while(n%primes[i]==0)
		{
			n=n/primes[i];
		}
	}
}
void soe()
{
	int prime[L]={0};
	int i,j;
	for(int i=2;i<L;i++)
	{
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
				prime[j]=1;
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		// for(auto it:now)
		// {
		// 	vis[it]=0;
		// }
		// now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pll pair <ll,ll>
#define nl "\n"
const ll L = 200005;
vector <ll> primes;
set <ll> now;
vector <ll> adj[L],fact[L];
ll maxi,node,ans,vis[L],a[L];
void dfs(ll x,ll p,ll dep,ll pri)
{
	vis[x]=1;
	now.insert(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(a[it]%pri==0)
			{
				dfs(it,x,dep+1,pri);
			}
		}
	}
}
void factorise(ll n,ll x)
{
	for(ll i=0;i<primes.size();i++)
	{
		if(n==0)
			break;
		if(n%primes[i]==0)
			fact[primes[i]].pb(x);
		while(n%primes[i]==0)
		{
			n=n/primes[i];
		}
	}
}
void soe()
{
	ll prime[L]={0};
	ll i,j;
	for(ll i=2;i<L;i++)
	{
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
				prime[j]=1;
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	ll n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
		{
			vis[it]=0;
		}
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pll pair <ll,ll>
#define nl "\n"
vector <ll> adj[300005];
ll maxi,node,val[300005],vis[300005],ans;
void dfs(ll x,ll p,ll dep,ll curr)
{
	vis[x]=1;
	if(dep>=maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(__gcd(curr,val[it])!=1)
			{
				dfs(it,x,dep+1,__gcd(curr,val[it]));
			}
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	ll n,i;
	cin>>n;
	for(i=1;i<=n;i++)
		cin>>val[i];
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=1;i<=n;i++)
	{
		if(!vis[i])
		{
			maxi=0;
			dfs(i,-1,0,val[i]);
			if(node!=i)
				ans=max(ans,maxi+1);
			else if(node==i && val[i]!=1)
				ans=max(ans,1);
			maxi=0;
			dfs(node,-1,0,val[node]);
			if(node==i && val[i]==1)
				continue;
			else
				ans=max(ans,maxi+1);
		}
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200001;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L],spf[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
		maxi=dep,node=x;
	for(auto it:adj[x])
		if(it!=p)
			if(a[it]%pri==0)
				dfs(it,x,dep+1,pri);
}
void factorise(int n,int x)
{
	while(n>1)
	{
		fact[spf[n]].pb(x);
		int ux=spf[n];
		while(n%ux==0)
			n=n/ux;
	}
}
void soe()
{
	int prime[L]={0};
	for(int i=1;i<L;i++)
		spf[i]=i;
	int i,j;
	for(int i=2;i<L;i++)
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
			{
				prime[j]=1;
				if(spf[j]==j)
					spf[j]=i;
			}
		}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
			vis[it]=0;
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define int long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L],spf[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
		if(it!=p)
			if(a[it]%pri==0)
				dfs(it,x,dep+1,pri);
}
void factorise(int n,int x)
{
	while(n>1)
	{
		fact[spf[n]].pb(x);
		int ux=spf[n];
		while(n%ux==0)
			n=n/ux;
		n=spf[n];
	}
}
void soe()
{
	int prime[L]={0};
	for(int i=1;i<L;i++)
		spf[i]=i;
	int i,j;
	for(int i=2;i<L;i++)
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
			{
				prime[j]=1;
				if(spf[j]==j)
					spf[j]=i;
			}
		}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
			vis[it]=0;
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define int long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(a[it]%pri==0)
			{
				dfs(it,x,dep+1,pri);
			}
		}
	}
}
void factorise(int n,int x)
{
	for(int i=0;i<primes.size();i++)
	{
		if(n==0)
			break;
		if(n%primes[i]==0)
		{
			fact[primes[i]].pb(x);
		}
		while(n%primes[i]==0)
		{
			n=n/primes[i];
		}
	}
}
void soe()
{
	int prime[L]={0};
	int i,j;
	for(int i=2;i<L/5;i++)
	{
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
				prime[j]=1;
		}
	}
	primes.pb(199999);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
		{
			vis[it]=0;
		}
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define int long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(a[it]%pri==0)
			{
				dfs(it,x,dep+1,pri);
			}
		}
	}
}
void factorise(int n,int x)
{
	for(int i=0;i<primes.size();i++)
	{
		if(n==0)
			break;
		if(n%primes[i]==0)
		{
			fact[primes[i]].pb(x);
		}
		while(n%primes[i]==0)
		{
			n=n/primes[i];
		}
	}
}
void soe()
{
	int prime[L]={0};
	int i,j;
	for(int i=2;i<L/100000;i++)
	{
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
				prime[j]=1;
		}
	}
	primes.pb(199999);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
		{
			vis[it]=0;
		}
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pll pair <ll,ll>
#define nl "\n"
vector <ll> adj[300005];
ll maxi=-1,node,val[300005],vis[300005],ans;
void dfs(ll x,ll p,ll dep,ll curr)
{
	vis[x]=1;
	if(dep>=maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(__gcd(curr,val[it])!=1)
			{
				dfs(it,x,dep+1,__gcd(curr,val[it]));
			}
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	ll n,i;
	cin>>n;
	for(i=1;i<=n;i++)
		cin>>val[i];
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=1;i<=n;i++)
	{
		maxi=0;
		if(!vis[i])
		{
			dfs(i,-1,0,val[i]);
			// cout<<i<<" "<<node<<" "<<maxi<<nl;
			maxi=0;
			dfs(node,-1,0,val[node]);
			// cout<<i<<" "<<node<<" "<<maxi<<nl;
			if(node==i && val[i]==1)
				continue;
			else
				ans=max(ans,maxi+1);
		}
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define int long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(a[it]%pri==0)
			{
				dfs(it,x,dep+1,pri);
			}
		}
	}
}
void factorise(int n,int x)
{
	for(int i=0;i<primes.size();i++)
	{
		if(n==0)
			break;
		if(n%primes[i]==0)
		{
			fact[primes[i]].pb(x);
		}
		while(n%primes[i]==0)
		{
			n=n/primes[i];
		}
	}
}
void soe()
{
	int prime[L]={0};
	int i,j;
	for(int i=2;i<L/10;i++)
	{
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
				prime[j]=1;
		}
	}
	primes.pb(199999);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
		{
			vis[it]=0;
		}
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define int long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(a[it]%pri==0)
			{
				dfs(it,x,dep+1,pri);
			}
		}
	}
}
void factorise(int n,int x)
{
	for(int i=0;i<primes.size();i++)
	{
		if(n==0)
			break;
		if(n%primes[i]==0)
		{
			fact[primes[i]].pb(x);
		}
		while(n%primes[i]==0)
		{
			n=n/primes[i];
		}
	}
}
void soe()
{
	int prime[L]={0};
	int i,j;
	for(int i=2;i<L/10000;i++)
	{
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
				prime[j]=1;
		}
	}
	primes.pb(199999);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
		{
			vis[it]=0;
		}
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L],spf[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
		maxi=dep,node=x;
	for(auto it:adj[x])
		if(it!=p)
			if(a[it]%pri==0)
				dfs(it,x,dep+1,pri);
}
void factorise(int n,int x)
{
	while(n>1)
	{
		fact[spf[n]].pb(x);
		int ux=spf[n];
		while(n%ux==0)
			n=n/ux;
		n=spf[n];
	}
}
void soe()
{
	int prime[L]={0};
	for(int i=1;i<L;i++)
		spf[i]=i;
	int i,j;
	for(int i=2;i<L;i++)
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
			{
				prime[j]=1;
				if(spf[j]==j)
					spf[j]=i;
			}
		}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
			vis[it]=0;
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define int long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(a[it]%pri==0)
			{
				dfs(it,x,dep+1,pri);
			}
		}
	}
}
void factorise(int n,int x)
{
	for(int i=0;i<primes.size();i++)
	{
		if(n==0)
			break;
		if(n%primes[i]==0)
			fact[primes[i]].pb(x);
		while(n%primes[i]==0)
		{
			n=n/primes[i];
		}
	}
}
void soe()
{
	int prime[L]={0};
	int i,j;
	for(int i=2;i<L/10;i++)
	{
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
				prime[j]=1;
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
		{
			vis[it]=0;
		}
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define int long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(a[it]%pri==0)
			{
				dfs(it,x,dep+1,pri);
			}
		}
	}
}
void factorise(int n,int x)
{
	for(int i=0;i<primes.size();i++)
	{
		if(n==0)
			break;
		if(n%primes[i]==0)
			fact[primes[i]].pb(x);
		while(n%primes[i]==0)
		{
			n=n/primes[i];
		}
	}
}
void soe()
{
	int prime[L]={0};
	int i,j;
	for(int i=2;i<L;i++)
	{
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
				prime[j]=1;
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
		{
			vis[it]=0;
		}
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define int long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L],spf[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(a[it]%pri==0)
			{
				dfs(it,x,dep+1,pri);
			}
		}
	}
}
void factorise(int n,int x)
{
	while(n>1)
	{
		// cout<<n<<" "<<spf[n]<<nl;
		fact[spf[n]].pb(x);
		int ux=spf[n];
		while(n%ux==0)
		{
			n=n/ux;
		}
		// cout<<n<<nl;
		n=spf[n];
	}
}
void soe()
{
	int prime[L]={0};
	for(int i=1;i<L;i++)
		spf[i]=i;
	int i,j;
	for(int i=2;i<L;i++)
	{
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
			{
				prime[j]=1;
				if(spf[j]==j)
					spf[j]=i;
			}
		}
	}
	// cout<<"finish"<<nl;
	// for(i=1;i<=100;i++)
	// 	cout<<spf[i]<<" ";
	// cout<<nl;
}
int main()
{
	// ios_base::sync_with_stdio(false);
	// cin.tie(0);
	// cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		// cout<<a[i]<<nl;
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
		{
			vis[it]=0;
		}
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define int long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L];
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(a[it]%pri==0)
			{
				dfs(it,x,dep+1,pri);
			}
		}
	}
}
void factorise(int n,int x)
{
	for(int i=0;i<primes.size();i++)
	{
		if(n==0)
			break;
		if(n%primes[i]==0)
		{
			fact[primes[i]].pb(x);
		}
		while(n%primes[i]==0)
		{
			n=n/primes[i];
		}
	}
}
void soe()
{
	int prime[L]={0};
	int i,j;
	for(int i=2;i<L/5;i++)
	{
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
				prime[j]=1;
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=0;i<primes.size();i++)
	{
		for(j=0;j<fact[primes[i]].size();j++)
		{
			if(!vis[fact[primes[i]][j]])
			{
				maxi=0;
				dfs(fact[primes[i]][j],-1,1,primes[i]);
				dfs(node,-1,1,primes[i]);
				if(node==fact[primes[i]][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
		{
			vis[it]=0;
		}
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pll pair <ll,ll>
#define nl "\n"
vector <ll> adj[300005];
ll maxi,node,val[300005],vis[300005],ans;
void dfs(ll x,ll p,ll dep,ll curr)
{
	vis[x]=1;
	if(dep>=maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(__gcd(curr,val[it])!=1)
			{
				dfs(it,x,dep+1,__gcd(curr,val[it]));
			}
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	ll n,i;
	cin>>n;
	for(i=1;i<=n;i++)
		cin>>val[i];
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(i=1;i<=n;i++)
	{
		if(!vis[i])
		{
			maxi=0;
			dfs(i,-1,0,val[i]);
			if(node!=i)
				ans=max(ans,maxi+1);
			else if(node==i && val[i]!=1)
				ans=max(ans,1ll);
			maxi=0;
			dfs(node,-1,0,val[node]);
			if(node==i && val[i]==1)
				continue;
			else
				ans=max(ans,maxi+1);
		}
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
// #define int long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pint pair <int,int>
#define nl "\n"
const int L = 200005;
vector <int> primes;
vector <int> now;
vector <int> adj[L],fact[L];
int maxi,node,ans,vis[L],a[L];
set <int> sprime;
void dfs(int x,int p,int dep,int pri)
{
	vis[x]=1;
	now.pb(x);
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(a[it]%pri==0)
			{
				dfs(it,x,dep+1,pri);
			}
		}
	}
}
void factorise(int n,int x)
{
	for(int i=0;i<primes.size();i++)
	{
		if(n==0)
			break;
		if(n%primes[i]==0)
		{
			fact[primes[i]].pb(x);
			sprime.insert(primes[i]);
		}
		while(n%primes[i]==0)
		{
			n=n/primes[i];
		}
	}
}
void soe()
{
	int prime[L]={0};
	int i,j;
	for(int i=2;i<L;i++)
	{
		if(!prime[i])
		{
			primes.pb(i);
			for(j=2*i;j<L;j+=i)
				prime[j]=1;
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n,i,j;
	soe();
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		factorise(a[i],i);
	}
	for(i=0;i<n-1;i++)
	{
		int u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	for(auto it:sprime)
	{
		for(j=0;j<fact[it].size();j++)
		{
			if(!vis[fact[it][j]])
			{
				maxi=0;
				dfs(fact[it][j],-1,1,it);
				dfs(node,-1,1,it);
				if(node==fact[it][j] && a[node]==1)
					continue;
				ans=max(ans,maxi);
			}
		}
		for(auto it:now)
		{
			vis[it]=0;
		}
		now.clear();
	}
	cout<<ans<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld double
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pll pair <ll,ll>
#define nl "\n"
vector <ll> adj[300005];
ll maxi=-1,node,val[300005];
void dfs(ll x,ll p,ll dep,ll curr)
{
	if(dep>maxi)
	{
		maxi=dep;
		node=x;
	}
	for(auto it:adj[x])
	{
		if(it!=p)
		{
			if(__gcd(curr,val[it])!=1)
			{
				dfs(it,x,dep+1,__gcd(curr,val[it]));
			}
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	ll n,i;
	cin>>n;
	for(i=0;i<n;i++)
		cin>>val[i];
	for(i=0;i<n-1;i++)
	{
		ll u,v;
		cin>>u>>v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	dfs(1,-1,0,val[1]);
	// cout<<node<<nl;
	dfs(node,-1,0,val[node]);
	if(node==1 && val[1]==1)
		cout<<0<<nl;
	else
		cout<<maxi+1<<nl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef map<int, int> mii;
typedef vector<mii> vmii;

const int MAXN = 2e5;

vvi pf;

void sieve()
{
    pf.resize(MAXN+1);

    for (int i = 2; i <= MAXN; ++i)
    {
        if (pf[i].empty())
        {
            for (int j = i; j <= MAXN; j += i)
            {
                pf[j].push_back(i);
            }
        }
    }
}

int n;
vi a;
vvi g;

void input()
{
    cin >> n;

    a.resize(n+1);
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }

    g.resize(n+1);
    for (int i = 0; i < n-1; ++i)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
}

vmii pft;

void tmplt()
{
    pft.resize(n+1);
    for (int i = 1; i <= n; ++i)
    {
        for (auto x: pf[a[i]])
        {
            pft[i].insert(pft[i].end(), {x, 0});
        }
    }
}

int gmax = 0;

int root;
vi pr;
void dfs(int z)
{
    if (g[z].size() == 1 && z != root)
    {
        for_each (pft[z].begin(), pft[z].end(), [](auto &x){x.second = 1;});
        // for (auto &p: pft[z])
        // {
        //     p.second = 1;
        // }

        if (a[z] != 1) gmax = max(gmax, 1);

        return;
    }

    for (auto y: g[z])
    {
        if (y == pr[z]) continue;

        pr[y] = z;
        dfs(y);
    }

    for (auto &fp: pft[z])
    {
        int mx1 = 0, mx2 = 0;
        for (auto y: g[z])
        {
            if (y == pr[z]) continue;

            // auto it = lower_bound(pft[y].begin(), pft[y].end(), fp, [](pii l, pii r){return l.first < r.first;});
            // if (it == pft[y].end()) break;
            // if (it->first != fp.first) continue;

            auto it = pft[y].find(fp.first);
            if (it == pft[y].end()) continue;

            int ct = it->second;
            if (ct > mx1)
            {
                mx2 = mx1;
                mx1 = ct;
            }
            else if (ct > mx2)
            {
                mx2 = ct;
            }
        }

        gmax = max(gmax, mx1 + mx2 + 1);
        fp.second = max(fp.second, mx1 + 1);
    }

}

void dfs_st()
{
    pr.resize(n+1);
    gmax = 0;

    for (int i = 1; i <= n; ++i)
    {
        if (g[i].size() <= 1)
        {
            root = i;
            break;
        }
    }

    dfs(root);
}

int main()
{
    sieve();
    input();
    tmplt();
    dfs_st();
    cout << gmax << endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vp;
typedef vector<vp> vvp;

const int MAXN = 2e5;

vvi pf;

void sieve()
{
    pf.resize(MAXN+1);

    for (int i = 2; i <= MAXN; ++i)
    {
        if (pf[i].empty())
        {
            for (int j = i; j <= MAXN; j += i)
            {
                pf[j].push_back(i);
            }
        }
    }
}

int n;
vi a;
vvi g;

void input()
{
    cin >> n;

    a.resize(n+1);
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }

    g.resize(n+1);
    for (int i = 0; i < n-1; ++i)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
}

vvp pft;

void tmplt()
{
    pft.resize(n+1);
    for (int i = 1; i <= n; ++i)
    {
        vp &p = pft[i];
        for (auto x: pf[a[i]])
        {
            p.push_back({x, 0});
        }
    }
}

int gmax = 0;

int root;
vi pr;
void dfs(int z)
{
    if (g[z].size() == 1 && z != root)
    {
        for (auto &p: pft[z])
        {
            p.second = 1;
        }

        if (a[z] != 1) gmax = max(gmax, 1);

        return;
    }

    for (auto y: g[z])
    {
        if (y == pr[z]) continue;

        pr[y] = z;
        dfs(y);
    }

    for (auto &fp: pft[z])
    {
        int mx1 = 0, mx2 = 0;
        bool f = false;
        for (auto y: g[z])
        {
            if (y == pr[z]) continue;

            auto it = lower_bound(pft[y].begin(), pft[y].end(), fp, [](pii l, pii r){return l.first < r.first;});
            if (it == pft[y].end()) break;
            if (it->first != fp.first) continue;

            int ct = it->second;
            if (ct > mx1)
            {
                mx2 = mx1;
                mx1 = ct;
            }
            else if (ct > mx2)
            {
                mx2 = ct;
            }
        }

        int k = a[z] == 1 ? 0 : 1;
        gmax = max(gmax, mx1 + mx2 + k);
        fp.second = max(fp.second, mx1 + k);
    }

}

void dfs_st()
{
    pr.resize(n+1);
    gmax = 0;

    for (int i = 1; i <= n; ++i)
    {
        if (g[i].size() == 1)
        {
            root = i;
            break;
        }
    }

    dfs(root);
}

int main()
{
    sieve();
    input();
    tmplt();
    dfs_st();
    cout << gmax << endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vp;
typedef vector<vp> vvp;

const int MAXN = 2e5;

vvi pf;

void sieve()
{
    pf.resize(MAXN+1);

    for (int i = 2; i <= MAXN; ++i)
    {
        if (pf[i].empty())
        {
            for (int j = i; j <= MAXN; j += i)
            {
                pf[j].push_back(i);
            }
        }
    }
}

int n;
vi a;
vvi g;

void input()
{
    cin >> n;

    a.resize(n+1);
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }

    g.resize(n+1);
    for (int i = 0; i < n-1; ++i)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
}

vvp pft;

void tmplt()
{
    pft.resize(n+1);
    for (int i = 1; i <= n; ++i)
    {
        vp &p = pft[i];
        for (auto x: pf[a[i]])
        {
            p.push_back({x, 0});
        }
    }
}

int gmax = 0;

int root;
vi pr;
void dfs(int z)
{
    if (g[z].size() == 1 && z != root)
    {
        for (auto &p: pft[z])
        {
            p.second = 1;
        }

        if (a[z] != 1) gmax = max(gmax, 1);

        return;
    }

    for (auto y: g[z])
    {
        if (y == pr[z]) continue;

        pr[y] = z;
        dfs(y);
    }

    for (auto &fp: pft[z])
    {
        int mx1 = 0, mx2 = 0;
        bool f = false;
        for (auto y: g[z])
        {
            if (y == pr[z]) continue;

            // auto it = lower_bound(pft[y].begin(), pft[y].end(), fp, [](pii l, pii r){return l.first < r.first;});
            // if (it == pft[y].end()) break;
            // if (it->first != fp.first) continue;

            auto it = find_if(pft[y].begin(), pft[y].end(), [&fp](pii x){return x.first == fp.first;});
            if (it == pft[y].end()) continue;

            int ct = it->second;
            if (ct > mx1)
            {
                mx2 = mx1;
                mx1 = ct;
            }
            else if (ct > mx2)
            {
                mx2 = ct;
            }
        }

        gmax = max(gmax, mx1 + mx2 + 1);
        fp.second = max(fp.second, mx1 + 1);
    }

}

void dfs_st()
{
    pr.resize(n+1);
    gmax = 0;

    for (int i = 1; i <= n; ++i)
    {
        if (g[i].size() == 1)
        {
            root = i;
            break;
        }
    }

    dfs(root);
}

int main()
{
    sieve();
    input();
    tmplt();
    dfs_st();
    cout << gmax << endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vp;
typedef vector<vp> vvp;

const int MAXN = 2e5;

vvi pf;

void sieve()
{
    pf.resize(MAXN+1);

    for (int i = 2; i <= MAXN; ++i)
    {
        if (pf[i].empty())
        {
            for (int j = i; j <= MAXN; j += i)
            {
                pf[j].push_back(i);
            }
        }
    }
}

int n;
vi a;
vvi g;

void input()
{
    cin >> n;

    a.resize(n+1);
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }

    g.resize(n+1);
    for (int i = 0; i < n-1; ++i)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
}

vvp pft;

void tmplt()
{
    pft.resize(n+1);
    for (int i = 1; i <= n; ++i)
    {
        vp &p = pft[i];
        for (auto x: pf[a[i]])
        {
            p.push_back({x, 0});
        }
    }
}

int gmax;

int root;
vi pr;
void dfs(int z)
{
    // cout << "d " << z << " entered" << endl;
    if (g[z].size() == 1 && z != root)
    {
        for (auto &p: pft[z])
        {
            p.second = 1;
        }

        // cout << "d " << z << " completed with pft[z] = ";
        // for (auto p: pft[z])
        // {
        //     cout << p.first << ": " << p.second << ", ";
        // }
        // cout << endl;

        return;
    }

    for (auto y: g[z])
    {
        if (y == pr[z]) continue;

        pr[y] = z;
        dfs(y);
    }

    for (auto &fp: pft[z])
    {
        int mx1 = 0, mx2 = 0;
        for (auto y: g[z])
        {
            if (y == pr[z]) continue;

            auto it = lower_bound(pft[y].begin(), pft[y].end(), fp, [](pii l, pii r){return l.first < r.first;});
            if (it->first != fp.first) continue;

            int ct = it->second;
            // cout << ct << " at it->first = " << it->first << " fp.first = " << fp.first << endl;
            if (ct > mx1)
            {
                mx2 = mx1;
                mx1 = ct;
            }
            else if (ct > mx2)
            {
                mx2 = ct;
            }
        }

        gmax = max(gmax, mx1 + mx2 + 1);
        // cout << "mx1 = " << mx1 << endl;
        fp.second = max(fp.second, mx1 + 1);
    }

    // cout << "d " << z << " completed with pft[z] = ";
    // for (auto p: pft[z])
    // {
    //     cout << p.first << ": " << p.second << ", ";
    // }
    // cout << endl;
}

void dfs_st()
{
    pr.resize(n+1);
    gmax = 0;

    for (int i = 1; i <= n; ++i)
    {
        if (g[i].size() == 1)
        {
            root = i;
            break;
        }
    }

    dfs(root);
}

int main()
{
    sieve();
    // cout << "s" << endl;
    input();
    // cout << "i" << endl;
    tmplt();
    // cout << "t" << endl;
    dfs_st();
    // cout << "d" << endl;
    cout << gmax << endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef map<int, int> mii;
typedef vector<mii> vmii;

const int MAXN = 2e5;

vvi pf;

void sieve()
{
    pf.resize(MAXN+1);

    for (int i = 2; i*i <= MAXN; ++i)
    {
        if (pf[i].empty())
        {
            for (int j = i; j <= MAXN; j += i)
            {
                pf[j].push_back(i);
            }
        }
    }
}

int n;
vi a;
vvi g;

void input()
{
    cin >> n;

    a.resize(n+1);
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }

    g.resize(n+1);
    for (int i = 0; i < n-1; ++i)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
}

vmii pft;

void tmplt()
{
    pft.resize(n+1);
    for (int i = 1; i <= n; ++i)
    {
        for (auto x: pf[a[i]])
        {
            pft[i].insert(pft[i].end(), {x, 0});
        }
    }
}

int gmax;

int root;
vi pr;
void dfs(int z)
{
    // cout << "d " << z << " entered" << endl;
    if (g[z].size() == 1 && z != root)
    {
        for_each (pft[z].begin(), pft[z].begin(), [](auto &x){x.second = 1;});
        // for (auto &p: pft[z])
        // {
        //     p.second = 1;
        // }

        // cout << "d " << z << " completed with pft[z] = ";
        // for (auto p: pft[z])
        // {
        //     cout << p.first << ": " << p.second << ", ";
        // }
        // cout << endl;

        return;
    }

    for (auto y: g[z])
    {
        if (y == pr[z]) continue;

        pr[y] = z;
        dfs(y);
    }

    for (auto &fp: pft[z])
    {
        int mx1 = 0, mx2 = 0;
        for (auto y: g[z])
        {
            if (y == pr[z]) continue;

            auto it = pft[y].find(fp.first);
            if (it->first != fp.first) continue;

            int ct = it->second;
            // cout << ct << " at it->first = " << it->first << " fp.first = " << fp.first << endl;
            if (ct > mx1)
            {
                mx2 = mx1;
                mx1 = ct;
            }
            else if (ct > mx2)
            {
                mx2 = ct;
            }
        }

        gmax = max(gmax, mx1 + mx2 + 1);
        // cout << "mx1 = " << mx1 << endl;
        fp.second = max(fp.second, mx1 + 1);
    }

    // cout << "d " << z << " completed with pft[z] = ";
    // for (auto p: pft[z])
    // {
    //     cout << p.first << ": " << p.second << ", ";
    // }
    // cout << endl;
}

void dfs_st()
{
    pr.resize(n+1);
    gmax = 0;

    for (int i = 1; i <= n; ++i)
    {
        if (g[i].size() == 1)
        {
            root = i;
            break;
        }
    }

    dfs(root);
}

int main()
{
    sieve();
    // cout << "s" << endl;
    input();
    // cout << "i" << endl;
    tmplt();
    // cout << "t" << endl;
    dfs_st();
    // cout << "d" << endl;
    cout << gmax << endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vp;
typedef vector<vp> vvp;

const int MAXN = 2e5;

vvi pf;

void sieve()
{
    pf.resize(MAXN+1);

    for (int i = 2; i <= MAXN; ++i)
    {
        if (pf[i].empty())
        {
            for (int j = i; j <= MAXN; j += i)
            {
                pf[j].push_back(i);
            }
        }
    }
}

int n;
vi a;
vvi g;

void input()
{
    cin >> n;

    a.resize(n+1);
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }

    g.resize(n+1);
    for (int i = 0; i < n-1; ++i)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
}

vvp pft;

void tmplt()
{
    pft.resize(n+1);
    pft[1].push_back({1,1});
    for (int i = 1; i <= n; ++i)
    {
        vp &p = pft[i];
        for (auto x: pf[a[i]])
        {
            p.push_back({x, 0});
        }
    }
}

int gmax;

int root;
vi pr;
void dfs(int z)
{
    // cout << "d " << z << " entered" << endl;
    if (g[z].size() == 1 && z != root)
    {
        for (auto &p: pft[z])
        {
            p.second = 1;
        }

        // cout << "d " << z << " completed with pft[z] = ";
        // for (auto p: pft[z])
        // {
        //     cout << p.first << ": " << p.second << ", ";
        // }
        // cout << endl;

        return;
    }

    for (auto y: g[z])
    {
        if (y == pr[z]) continue;

        pr[y] = z;
        dfs(y);
    }

    for (auto &fp: pft[z])
    {
        int mx1 = 0, mx2 = 0;
        for (auto y: g[z])
        {
            if (y == pr[z]) continue;

            auto it = lower_bound(pft[y].begin(), pft[y].end(), fp, [](pii l, pii r){return l.first < r.first;});
            if (it->first != fp.first) continue;

            int ct = it->second;
            // cout << ct << " at it->first = " << it->first << " fp.first = " << fp.first << endl;
            if (ct > mx1)
            {
                mx2 = mx1;
                mx1 = ct;
            }
            else if (ct > mx2)
            {
                mx2 = ct;
            }
        }

        gmax = max(gmax, mx1 + mx2 + 1);
        // cout << "mx1 = " << mx1 << endl;
        fp.second = max(fp.second, mx1 + 1);
    }

    // cout << "d " << z << " completed with pft[z] = ";
    // for (auto p: pft[z])
    // {
    //     cout << p.first << ": " << p.second << ", ";
    // }
    // cout << endl;
}

void dfs_st()
{
    pr.resize(n+1);
    gmax = 0;

    for (int i = 1; i <= n; ++i)
    {
        if (g[i].size() == 1)
        {
            root = i;
            break;
        }
    }

    dfs(root);
}

int main()
{
    sieve();
    // cout << "s" << endl;
    input();
    // cout << "i" << endl;
    tmplt();
    // cout << "t" << endl;
    dfs_st();
    // cout << "d" << endl;
    cout << gmax << endl;
}


#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vp;
typedef vector<vp> vvp;

const int MAXN = 2e5;

vvi pf;

void sieve()
{
    pf.resize(MAXN+1);

    for (int i = 2; i <= MAXN; ++i)
    {
        if (pf[i].empty())
        {
            for (int j = i; j <= MAXN; j += i)
            {
                pf[j].push_back(i);
            }
        }
    }
}

int n;
vi a;
vvi g;

void input()
{
    cin >> n;

    a.resize(n+1);
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }

    g.resize(n+1);
    for (int i = 0; i < n-1; ++i)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
}

vvp pft;

void tmplt()
{
    pft.resize(n+1);
    for (int i = 1; i <= n; ++i)
    {
        vp &p = pft[i];
        for (auto x: pf[a[i]])
        {
            p.push_back({x, 0});
        }
    }
}

int gmax;

int root;
vi pr;
void dfs(int z)
{
    // cout << "d " << z << " entered" << endl;
    if (g[z].size() == 1 && z != root)
    {
        for (auto &p: pft[z])
        {
            p.second = 1;
        }

        // cout << "d " << z << " completed with pft[z] = ";
        // for (auto p: pft[z])
        // {
        //     // cout << p.first << ": " << p.second << ", ";
        // }
        // cout << endl;

        return;
    }

    // cout << "93" << endl;

    for (auto y: g[z])
    {
        if (y == pr[z]) continue;

        pr[y] = z;
        dfs(y);
    }

    // cout << "pft[z].size() = " << pft[z].size() << endl;
    // cout << "hey " << z << endl;
    for (auto &fp: pft[z])
    {
        int mx1 = 0, mx2 = 0;
        bool f = false;
        for (auto y: g[z])
        {
            if (y == pr[z]) continue;

            // cout << "112" << endl;
            auto it = lower_bound(pft[y].begin(), pft[y].end(), fp, [](pii l, pii r){return l.first < r.first;});
            // cout << "114" << endl;
            if (it == pft[y].end())
            {
                f = true;
                break;
            }
            if (it->first != fp.first) continue;

            // cout << "117" << endl;
            int ct = it->second;
            // // cout << ct << " at it->first = " << it->first << " fp.first = " << fp.first << endl;
            if (ct > mx1)
            {
                mx2 = mx1;
                mx1 = ct;
            }
            else if (ct > mx2)
            {
                mx2 = ct;
            }
        }
        if (f) break;

        gmax = max(gmax, mx1 + mx2 + 1);
        // // cout << "mx1 = " << mx1 << endl;
        fp.second = max(fp.second, mx1 + 1);
    }

    // cout << "d " << z << " completed with pft[z] = ";
    // for (auto p: pft[z])
    // {
    //     // cout << p.first << ": " << p.second << ", ";
    // }
    // cout << endl;
}

void dfs_st()
{
    pr.resize(n+1);
    gmax = 0;

    for (int i = 1; i <= n; ++i)
    {
        if (g[i].size() == 1)
        {
            root = i;
            break;
        }
    }

    // cout << "r" << endl;
    dfs(root);
}

int main()
{
    // cout << "e" << endl;
    sieve();
    // cout << "s" << endl;
    input();
    // cout << "i" << endl;
    tmplt();
    // cout << "t" << endl;
    dfs_st();
    // cout << "d" << endl;
    cout << gmax << endl;
}