///████▓▓▓▓▓▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
///████▓▓▓▓▓▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒░░▒▒▓▓▓▓▓▒░░░░░░▒▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓██▓
///███▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒░▒▒▓▓██▓▓▒▒▓▓▓░░░░░░░░▒▓▓▓▓▓▓▓▓▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
///██▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▓▓▒▓▓██▓▓▒▓▓▓▓███▒░░░░░░░░▒▓▓▓▓▓▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
///█▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▓▓▓▓▓█▓▒▒▓▓▓▓██████░░░░░░░░░▒▓▓▓▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
///▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▓██▓▓▓█▓▒▓▓▓█████████▓░░░░░░░░░▒▒▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓
///▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▓█▓▓▓▓▓▓▒▓█████████████▒░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▓█▓▓▓▓▓▒▒▒▓██████████████░░▒▒░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▒▓▓▒▒▒▒▒▒▒▒▒
///▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▓▓██▓▒▒▒▒▒▒▓█████████████▒░░░▒░░░▒░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▒▒▒▒▒▒▒▒▒
///▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▓▓██▓▒▒░░▒▒▒▒▓████████████▒▒░▒░░░░░▒░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▒▒▒▒▒▒▒
///▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒████▒▒░░░░░▒▒▓▓███████████▓░░▒▒▒░░░▒░░░░░▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒
///▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▒▒▒▓███▓▒░░░░░░░▒▒▓████████████░░░▒▒░░░▒▒░░░░░▒▒▓▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▒▒▒▒
///▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒▓███▒▒░░░░░░░▒▒▓████████████▒▒░░▒░░░▒░░░░░░░▒▒▒▓▓▓▓▓▓▒▒▓▓▓▓▓▓▓▒▒▒▒▒▒
///▓▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓███▓▒▒▒░░░░░▒░░▒▓█▓█████████▓░░░░░░░▒▒░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒▒▒▒
///▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▒███▓▓▓▒░░░░░░░▒▓▓▓█▓█████████░▒░░░░░▒▒▒▒░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▒▒▒▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▒▓███▓▒▓▓░░░░▒░▒▒▒▓▓█▓█████████▓░░░▒▒▒░▒▒▒░░░░░░░▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▒▒▓▓▓
///▒▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▓▓▒▒▒▒▒▒▒▓▓▓▓▒▓█▓▓▒░░░░░░▒▒▒░░░░▒▓▓▓█████████░▒░▒▒▒░▒▒░░░░░░░░░▒▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
///▓▓▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▓▓▓▓▓▒▒▓▓▓▓▓▓▒██▓▒▒░░░░░░▒▒▒░░░░▒▒▓▓█████████▒▒░▒▒▒▒▒▒▒░░░░░░░░▒▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
///▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▓▓▓▓▒▒██▓▒░░░░░░░▒▒▒▒░░░░▒▒▓█████████▓░░▒▒▒▒▒▒▒░▒░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓▓▓▒▒▒░░░▒█▓░░░░░░░░▒▒▒▒░░░░▒▒▒██████████░░▒▒▒▒▒▒░░▒░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▒▒░░░░░░▒▒▓██▒░░░░░░░░▒▒░░░░░▒▒▒▓█████████░▒▒▒▒▒░░░▒▒▒░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▓▓▓▒░▒▒▒▒▒▒▒▒▒▓▓███░░░░░░░░▒░░░░░▒▒▒▒▓████████▒▒▒▒▒▒░░▒▒▒▒▒░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▓▒░▒▒▒▒▒▒▓▓▓▓▒▒▓▓██▓░░░▒▒░░▒▒▒░░░▒▒▒▓▓███████▓░▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▒▒▒▒▓▓▓▒▒▒▒▒▒▒▒▒▒░░▒▓▒▒▒▒▒▓▓▓▓▓▓▓▓██▓░░▒▓▓▒▒▒▒▒░░░░▒▒▓██████▒░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▒▒▒▒▒▓▓▒▒▒▒▒▒▒▒▒▒░░░▒▒▒▒░▒▒▓▒▒▒▓▓▓▓██▓░░░░░░░░░░░░░░░░░░░░▒░▒▒▒▒▒▒▓▓▒▒▒▓▒▒▒▒░▒▒░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▒▒▒▒▒▓▓▓▓▒▒▒▒▒▒▒▒▒░░░▒▒▒▒▒▒▒▒▓▒▓▓▓▓█▓░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▓▒▒░▒▒░▒▒▒▒▒░▒▒░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▒▒▒▒▓▓▓▓▓▒▒▒▒▒▒▒▒▓░░░░▒▓▒▒▒▒▒▓▓▓▓▓▓▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒░░░▒▒▒▒▓▒▒▒▓█▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▒▓▓▓▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒░░░░▒▓▓▓▓▓▓▓▓▓▓▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒░░░▓████████▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒░░░░▒████████▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒░░░░▒███████▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒░░░░░▓██████▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▓██████▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒░░░░░░▒▓███▓█▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▒░▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒░░░░░░░░░▒▓▓█▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▒░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░▒░░░░░░▒▒▒▒▒░▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒░░░░░░░░░░▒▓█▓▓▓█▓▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▒░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░▒▒▒▒░▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒░░░░░░░░░░░░▒▓▓▓█▓█▓▒▒▒▒▒▒▒▒▒▒▒▒▒▒
///▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░▒▒▒▒▒░▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▓▓██▓█▓▒░░░▒▒▒▒▒▒▒▒▒
///▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▒▓███▓█▓▒▒▒▒▒▒▒▒▒▒▒▒▒
///▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▒▓▓████▓▓█▓▒▒▒▒▒▒▒▒▒▒▒▒
///▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▓▓▓▓██████▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒
///▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▒░░░░░░░░░░░░░░░░░░░░░░▒▒▒▒▓███████▓▓▓▓▓▒▒▒▒▒▒▒▓▓▒
///▒▒▒▒░░░░░░░░░░▒▒░▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░▒▒▓▒▒▒▓████████▓▓██▓▓▓▒▒▒▒▓▓▒▒
///▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░▒▓▒▒▒▒█████████▓▒▒▓█▓▓▒▒▒▒▒▒▒▒
///▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░░░░░▒▒▒▒▒▓████████▓▒▒▒▒▓▓▓▓▒▒▒▒▒▒▒
///▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░░▒▒▒▓▒▒░░░░░░░░░░░░░░░░░░░░░░░▒▓▒▒▒▒███████▓▒▒░░░░▓█▓▓▒▒▒▒▒▒
///▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░░▒▒▒▒▓██▒░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▒▒▒███████▒▒▒░░░░▒▒█▓▓▓▒▒▒▒
///▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░▒▒▒▒▒▒███▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▒▒██████▒░▒░░░▒▒▒▓▓▓▓▒▒▓▒
///▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░░▒▒▒▒▒▒▓▓███▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▓████▒▒▒░░░░▒▒▓███▓▓▓▒
///▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░░░░░░░░▒▒▒▒▒▒▒▓▓▓█▓██░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒░░░░░░▒▒███▓▒▒░░░▒▓▓███▓▓▓▓
///▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓█▓████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒░░░░░░▓██▓▓▒▒▒▒▒█████▓▒▒
///▒▒▒▒▒▒▒▒▒▒▒░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▓█▒███░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▒▒░░▒▒▓██▓▓▒▒▒▓████▓▓▓
///▒▒▒▒▒▒▒▒▒░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▒▒▓▓▓█▓███▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒░▒▒▒▒▒▒▓██▓▒▓█████▓▓
///▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▓▓▓▓▒▒▒▒▒▓▓█████▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒░▓████▓▓
///▒▒▒▒▒▒░░░░░░░░▒▒▒▒▒▒▒▒▒▓▓▓▓▒▒▒▒▒▒▒▒▒▒▓▓█████▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒▓█████
///▒▒▒▒▒░░░░░░░▒▒▒▒▒▒▒▒▒▓▓▓▓▒▒▒▒▒▒▒▒▒▒▒▓▓██████▒░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▒▒██▓██
#include <bits/stdc++.h>
using namespace std;
#define Task "Test"
#define int long long
#define el '\n'
#define cnt_bit_1 __builtin_popcountll
#define float double
#define IO freopen(Task".inp","r",stdin); freopen(Task".out","w",stdout);
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
const int M=2500005;
const int N=3e5+5;
const int INF=1e18;
const int MOD=1e9+7;
int n , q;
struct Query {
int type , x , y;
} qr[N];
int c[N] , dist[N];
vector<int> ke[N];
void bfs(int st)
{
memset(dist , -1 , sizeof(dist));
queue<int> qu;
qu.push(st);
dist[st] = 0;
while(!qu.empty())
{
int u = qu.front();
qu.pop();
for(int v : ke[u])
{
if(dist[v] == -1)
{
dist[v] = dist[u] + 1;
qu.push(v);
}
}
}
}
void sub1()
{
for(int i = 1 ; i <= q ; i++)
{
if(qr[i].type == 1)
{
int x = qr[i].x , y = qr[i].y;
for(int u = 1 ; u <= n ; u++)
{
for(int v = 1 ; v <= n ; v++)
{
if(u != v)
{
if((c[u] == x && c[v] == y) || (c[u] == y && c[v] == x)) ke[u].pb(v);
}
}
}
}
else
{
int u = qr[i].x , v = qr[i].y;
for(int j = 1 ; j <= n ; j++)
{
if(c[j] == u) c[j] = v;
}
}
}
bfs(1);
for(int i = 1 ; i <= n ; i++) cout << dist[i] << " ";
}
vector<pii> ke2[3 * N];
int dist2[3 * N];
void bfs01(int st)
{
for(int i = 0 ; i < 3 * N ; i++) dist2[i] = INF;
deque<int> dq;
dist2[st] = 0;
dq.push_front(st);
while(!dq.empty())
{
int u = dq.front();
dq.pop_front();
for(auto edge : ke2[u])
{
int v = edge.fi , w = edge.se;
if(dist2[v] > dist2[u] + w)
{
dist2[v] = dist2[u] + w;
if(w == 0) dq.push_front(v);
else dq.pb(v);
}
}
}
}
void sub2()
{
for(int i = 1 ; i <= n ; i++)
{
int in = n + c[i] , out = 2 * n + c[i];
ke2[in].pb({i , 0});
ke2[i].pb({out , 0});
}
for(int i = 1 ; i <= q ; i++)
{
if(qr[i].type == 1)
{
int x = qr[i].x , y = qr[i].y;
ke2[2 * n + x].pb({n + y , 1});
ke2[2 * n + y].pb({n + x , 1});
}
}
bfs01(1);
for(int i = 1 ; i <= n ; i++)
{
if(dist2[i] == INF) cout << -1 << " ";
else cout << dist2[i] << " ";
}
}
void sub3()
{
vector<vector<int>> lst(N);
for(int i = 1 ; i <= n ; i++) lst[c[i]].pb(i);
for(int i = 1 ; i <= q ; i++)
{
if(qr[i].type == 2)
{
int u = qr[i].x , v = qr[i].y;
if(u == v || lst[u].empty()) continue;
if(lst[u].size() > lst[v].size()) lst[u].swap(lst[v]);
for(int node : lst[u]) lst[v].pb(node);
lst[u].clear();
}
}
for(int col = 1 ; col < N ; col++)
{
for(int node : lst[col]) c[node] = col;
}
sub2();
}
vector<pii> ke4[M];
int dist4[M] , headn[N] , tailn[N];
int ncnt;
void bfs01_sub4(int st)
{
for(int i = 1 ; i <= ncnt ; i++) dist4[i] = INF;
deque<int> dq;
dist4[st] = 0;
dq.push_front(st);
while(!dq.empty())
{
int u = dq.front();
dq.pop_front();
for(auto edge : ke4[u])
{
int v = edge.fi , w = edge.se;
if(dist4[v] > dist4[u] + w)
{
dist4[v] = dist4[u] + w;
if(w == 0) dq.push_front(v);
else dq.pb(v);
}
}
}
}
void sub4()
{
ncnt = n;
for(int i = 1 ; i <= n ; i++)
{
headn[i] = ++ncnt;
tailn[i] = ++ncnt;
}
for(int i = 1 ; i <= n ; i++)
{
ke4[headn[c[i]]].pb({i, 0});
ke4[i].pb({tailn[c[i]], 0});
}
for(int i = 1 ; i <= q ; i++)
{
if(qr[i].type == 1)
{
int x = qr[i].x, y = qr[i].y;
ke4[tailn[x]].pb({headn[y] , 1});
ke4[tailn[y]].pb({headn[x] , 1});
}
else
{
int x = qr[i].x , y = qr[i].y;
if(x == y) continue;
int nwx = ++ncnt , ntx = ++ncnt;
int nhy = ++ncnt , nwy = ++ncnt;
ke4[nhy].pb({headn[x], 0});
ke4[nhy].pb({headn[y], 0});
ke4[tailn[x]].pb({nwy, 0});
ke4[tailn[y]].pb({nwy, 0});
headn[x] = nwx;
tailn[x] = ntx;
headn[y] = nhy;
tailn[y] = nwy;
}
}
bfs01_sub4(1);
for(int i = 1 ; i <= n ; i++)
{
if(dist4[i] == INF) cout << -1 << " ";
else cout << dist4[i] << " ";
}
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//IO
cin >> n >> q;
for(int i = 1 ; i <= n ; i++) cin >> c[i];
bool ok1 = true , check1 = false , ok2 = true;
for(int i = 1 ; i <= q ; i++)
{
cin >> qr[i].type >> qr[i].x >> qr[i].y;
if(qr[i].type == 1) check1 = true;
else
{
ok1 = false;
if(check1) ok2 = false;
}
}
if(n <= 100 && q <= 100) sub1();
else if(ok1) sub2();
else if(ok2) sub3();
else sub4();
return 0;
}