#include #include using namespace std; using namespace atcoder; #define MOD 1000000007 #define MOD9 998244353 #define pb push_back #define yes "Yes" #define no "No" #define takah "Takahashi" #define aoki "Aoki" #define ALL(x) (x).begin(), (x).end() template bool chmax(T &x, const T &y){if (x < y) {x = y; return true;} return false;} template bool chmin(T &x, const T &y){if (x > y) {x = y; return true;} return false;} template void print(T x){cout << x << endl;} int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector ufts(30, dsu(2 * N)); int A, B, Y; for (int hito = 0; hito < M; hito++){ cin >> A >> B >> Y; A--; B--; for (int i = 0; i < 30; i++){ if (Y & 1){ if (ufts[i].same(A, B)){ print(-1); return 0; } ufts[i].merge(A, B + N); ufts[i].merge(A + N, B); } else { if (ufts[i].same(A + N, B)){ print(-1); return 0; } ufts[i].merge(A, B); ufts[i].merge(B, A); } Y >>= 1; } } int pow2 = 1; vector X(N, 0); for (int k = 0; k < 30; k++){ vector x(N, 0); for (int i = 0; i < N; i++){ if (ufts[k].size(i) == 1) continue; int r = ufts[k].leader(i); if (r < N){ x[i] = x[r]; X[i] += pow2 * x[r]; } else { r -= N; x[i] = 1 ^ x[r]; X[i] += pow2 * (1 ^ x[r]); } } pow2 *= 2; } for (int i = 0; i < N; i++){ print(X[i]); } }