#include<bits/stdc++.h>
const int N=1.5e5+77;
char buf[20000007],*ptr=buf;
int _(){
int x=0;
while(*ptr<48)++ptr;
while(*ptr>47)x=x*10+*ptr++-48;
return x;
}
int gf(int*f,int x){
while(x!=x[f])x=x[f]=x[f][f];
return x;
}
int n,m,v[N],qs[N*5][3],f[N],g[N],id[N],idp=0;
int es[N*2],enx[N*2],e0[N],ep=2;
int fa[N],dep[N],sz[N],son[N],top[N];
int tr[533333],mx;
void ae(int a,int b){
es[ep]=b;enx[ep]=e0[a];e0[a]=ep++;
es[ep]=a;enx[ep]=e0[b];e0[b]=ep++;
}
void add(int a,int b){
for(int w=mx+id[a];w;w>>=1)tr[w]+=b;
}
int sum(int l,int r){
int s=0;
for(l+=mx-1,r+=mx+1;r-l!=1;l>>=1,r>>=1){
if(~l&1)s+=tr[l+1];
if(r&1)s+=tr[r-1];
}
return s;
}
void f1(int w,int pa){
dep[w]=dep[fa[w]=pa]+(sz[w]=1);
for(int i=e0[w];i;i=enx[i]){
int u=es[i];
if(u==pa)continue;
f1(u,w);
sz[w]+=sz[u];
if(sz[u]>sz[son[w]])son[w]=u;
}
}
void f2(int w,int tp){
id[w]=++idp;
tr[mx+idp]=v[w];
top[w]=tp;
if(son[w])f2(son[w],tp);
for(int i=e0[w];i;i=enx[i]){
int u=es[i];
if(u!=fa[w]&&u!=son[w])f2(u,u);
}
}
int query(int x,int y){
int a=top[x],b=top[y],s=0;
while(a!=b){
if(dep[a]<dep[b])std::swap(x,y),std::swap(a,b);
s+=sum(id[a],id[x]);
x=fa[a],a=top[x];
}
if(dep[x]<dep[y])std::swap(x,y);
s+=sum(id[y],id[x]);
if(g[y]!=y)y=gf(g,y),s+=tr[mx+id[y]];
return s;
}
void mg(int x,int y){
int s=0;
x=gf(g,x),y=gf(g,y);
while(x!=y){
if(dep[x]<dep[y])std::swap(x,y);
s+=tr[mx+id[x]];
add(x,-tr[mx+id[x]]);
x=g[x]=gf(g,fa[x]);
}
if(s)add(x,s);
}
int main(){
fread(buf,1,sizeof(buf),stdin);
n=_();m=_();
for(mx=1;mx<=n+5;mx<<=1);
for(int i=1;i<=n;++i)v[i]=_(),f[i]=g[i]=i;
for(int i=1;i<=m;++i){
qs[i][0]=_();
qs[i][1]=_();
qs[i][2]=_();
if(qs[i][0]==1){
int x=gf(f,qs[i][1]),y=gf(f,qs[i][2]);
if(x!=y)f[x]=y,ae(qs[i][1],qs[i][2]);
}
}
for(int i=1;i<=n;++i)if(!id[i])f1(i,0),f2(i,i);
for(int i=1;i<=n;++i)f[i]=i;
for(int i=mx-1;i;--i)tr[i]=tr[i<<1]+tr[(i<<1)+1];
for(int i=1;i<=m;++i){
int o=qs[i][0],a=qs[i][1],b=qs[i][2];
if(o==1){
int x=gf(f,a),y=gf(f,b);
if(x!=y)f[x]=y;
else mg(a,b);
}else if(o==2){
add(gf(g,a),b-v[a]);
v[a]=b;
}else{
int x=gf(f,a),y=gf(f,b);
if(x!=y)puts("-1");
else printf("%d\n",query(gf(g,a),gf(g,b)));
}
}
return 0;
}