#include #define ALL(x) (x).begin(), (x).end() #define SZ(x) (int) (x).size() #define pb push_back #define fi first #define se second using namespace std; typedef long long LL; typedef vector VI; typedef pair PII; typedef long double LD; const int MN = (int)1e5 + 5, P2 = (1 << 17), MTS = 2 * P2 + 5; int ans[MTS]; VI T[MTS]; int ile[MN], par[MN], typ[MN], V[MN], S[MN]; map < PII, int > bylo; PII E[MTS]; int curV, ost; int Find(int x) { while(x != par[x]) x = par[x]; return x; } void Union(int a, int b) { ++ost; a = Find(a); b = Find(b); if(a == b) { S[ost] = -1; return; } if(ile[a] > ile[b]) { ile[a] += ile[b]; par[b] = a; S[ost] = b; } else { ile[b] += ile[a]; par[a] = b; S[ost] = a; } } void cof(int a) { if(S[ost] == -1) { --ost; return; } a = Find(a); par[S[ost]] = S[ost]; ile[a] -= ile[S[ost]]; --ost; } void add(int a, int b, int v) { a += P2, b += P2; while(a < b) { if(a & 1) T[a++].pb(v); if(!(b & 1)) T[b--].pb(v); a >>= 1, b >>= 1; } if(a == b) T[a].pb(v); } void solve(int x) { while(T[x].size() && V[T[x].back()] > curV) T[x].pop_back(); int s = T[x].size(); for(int i = 0; i < s; ++i) Union(E[T[x][i]].fi, E[T[x][i]].se); if(x >= P2) { int p = x - P2; if(Find(E[p].fi) == Find(E[p].se)) ans[p] = curV; } else { solve(2 * x); solve(2 * x + 1); } for(int i = s - 1; i >= 0; --i) cof(E[T[x][i]].fi); } bool comp(int a, int b) { return (V[a] < V[b]); } int main() { int n, q; scanf("%d%d", &n, &q); for(int i = 1; i <= n; ++i) { par[i] = i; ile[i] = 1; } for(int i = 1; i <= q; ++i) { int t, x, y, v; scanf("%d", &t); typ[i] = t; if(t == 0) { scanf("%d%d%d", &x, &y, &v); ++x, ++y; E[i] = {x, y}; V[i] = v; bylo[E[i]] = i; } if(t == 1) { scanf("%d%d", &x, &y); ++x, ++y; E[i] = {x, y}; add(bylo[E[i]], i, bylo[E[i]]); bylo[E[i]] = 0; } if(t == 2) { scanf("%d%d", &x, &y); ++x, ++y; E[i] = {x, y}; ans[i] = -1; } } for(int i = 1; i <= q; ++i) if(typ[i] == 0 && bylo[E[i]] != 0) { add(bylo[E[i]], q + 1, bylo[E[i]]); bylo[E[i]] = 0; } for(int i = 1; i < MTS; ++i) sort(T[i].begin(), T[i].end(), comp); for(int i = 9; i >= 0; --i) { curV = i; solve(1); } for(int i = 1; i <= q; ++i) if(typ[i] == 2) printf("%d\n", ans[i]); return 0; }