#include using namespace std; using ll=long long; using ld=long double; using vi=vector; using ii=pair; #define FOR(i,a,b) for(int i=a; i < b; i++) #define F(n) FOR(i, 0, n) #define FF(n) FOR(j, 0, n) #define aa first #define bb second #define PB push_back struct nd { typedef nd nt; nd():P(0),pp(0),f(0){*C=C[1]=0;fix();} nt*P,*C[2],*pp; int f; void fix() { if(*C)C[0]->P=this; if(C[1])C[1]->P=this; } void psh() { if(!f)return; f=0,swap(*C,C[1]); if(*C)C[0]->f^=1; if(C[1])C[1]->f^=1; } int up(){return !P?-1:!(P->C[0]==this);} void zig(int c){ nt*X=C[c]; if((X->P=P))P->C[up()]=X; C[c]=X->C[1-c],X->C[1-c]=this; fix();X->fix(); if(P)P->fix(); swap(pp,X->pp); } void zA(int c){ nt*X=C[c],*Y=X->C[c]; if((Y->P=P))P->C[up()]=Y; C[c]=X->C[1-c],X->C[c]=Y->C[1-c],Y->C[1-c]=X; X->C[1-c]=this,fix(),X->fix(),Y->fix(); if(P)P->fix(); swap(pp,Y->pp); } void zB(int c){ nt*X=C[c],*Y=X->C[1-c]; if ((Y->P=P))P->C[up()]=Y; C[c]=Y->C[1-c],X->C[1-c]=Y->C[c],Y->C[1-c]=this; Y->C[c]=X,fix(),X->fix(),Y->fix(); if(P)P->fix(); swap(pp,Y->pp); } nt* splay() { for(psh();P;){ if(P->P)P->P->psh(); P->psh(),psh(); int c1=up(), c2=P->up(); if (!~c2) P->zig(c1); else if (c1==c2)P->P->zA(c1); else P->P->zB(c2); } return this; } nt*lst(){return psh(),C[1]?C[1]->lst():splay();} nt*fst(){return psh(),*C?C[0]->fst():splay();} }; struct lc { typedef nd nt; void lctr(int N){cd.resize(N);} void lnk(int u, int v) {if(con(u,v))return;rt(v),cd[v].pp=&cd[u];} void cut(int u, int v) { if(!con(u,v))return; rt(u),cd[v].splay(); if(cd[v].pp)cd[v].pp=0; else cd[v].C[0]=cd[v].C[0]->P=0,cd[v].fix(); } bool con(int u, int v) { nt*nu=acc(u)->fst(),*nv=acc(v)->fst(); return nu==nv; } void rt(int u) { acc(u),cd[u].splay(); if(*cd[u].C) cd[u].C[0]->P=0,cd[u].C[0]->f^=1,cd[u].C[0]->pp=&cd[u], cd[u].C[0]=0,cd[u].fix(); } nd* acc(int u){ nt*x,*pp; for(x=cd[u].splay();x->pp;x=pp){ pp=x->pp->splay();x->pp=0; if(pp->C[1])pp->C[1]->P=0,pp->C[1]->pp=pp; pp->C[1]=x,pp->fix(); } return x; } vector cd; }; lc lcts[10]; int main() { ios::sync_with_stdio(0); int n, q; cin >> n >> q; F(10) { lcts[i].lctr(n); } F(q) { int t; cin >> t; if (t==0) { int x, y, v; cin >> x >> y >> v; for (int j = v; j < 10; j++) { lcts[j].lnk(x, y); } } else if (t==1) { int x, y; cin >> x >> y; for (int j = 0; j < 10; j++) { lcts[j].cut(x, y); } } else if (t==2) { int x, y; cin >> x >> y; int path = -1; for (int j = 0; j < 10; j++) { if (lcts[j].con(x,y)) { path = j; break; } } cout << path << endl; } } return 0; }