#include using namespace std; struct strongly_connected_components{ vector> ans; vector scc; void dfs1(vector> &E, vector &t, vector &used, int v){ for (int w : E[v]){ if (!used[w]){ used[w] = true; dfs1(E, t, used, w); } } t.push_back(v); } void dfs2(vector> &E2, vector &used2, int v){ ans.back().push_back(v); for (int w : E2[v]){ if (!used2[w]){ used2[w] = true; dfs2(E2, used2, w); } } } strongly_connected_components(vector> &E){ int N = E.size(); vector> E2(N); for (int i = 0; i < N; i++){ for (int j : E[i]){ E2[j].push_back(i); } } vector t; vector used(N, false); for (int i = 0; i < N; i++){ if (!used[i]){ used[i] = true; dfs1(E, t, used, i); } } reverse(t.begin(), t.end()); vector used2(N, false); scc = vector(N); int cnt = 0; for (int i = 0; i < N; i++){ if (!used2[t[i]]){ used2[t[i]] = true; ans.push_back(vector()); dfs2(E2, used2, t[i]); for (int j : ans.back()){ scc[j] = cnt; } cnt++; } } } int operator [](int k){ return scc[k]; } int size(){ return ans.size(); } }; int main(){ int N, M; cin >> N >> M; vector p(M), q(M), a(M), b(M); for (int i = 0; i < M; i++){ cin >> p[i] >> q[i] >> a[i] >> b[i]; p[i]--; q[i]--; a[i]--; b[i]--; } vector> E(N * 6); for (int i = 0; i < N; i++){ for (int j = 0; j < 3; j++){ for (int k = j + 1; k < 3; k++){ int x = i * 3 + j; int y = i * 3 + k; E[x].push_back(N * 3 + y); E[y].push_back(N * 3 + x); } } } for (int i = 0; i < M; i++){ int x = p[i] * 3 + a[i]; int y = q[i] * 3 + b[i]; E[N * 3 + x].push_back(y); E[N * 3 + y].push_back(x); } strongly_connected_components G(E); bool ok = true; for (int i = 0; i < N * 3; i++){ if (G[i] == G[N * 3 + i]){ ok = false; } } if (!ok){ cout << -1 << endl; } else { vector x(N); for (int i = 0; i < N; i++){ for (int j = 0; j < 3; j++){ int v = i * 3 + j; if (G[v] > G[N * 3 + v]){ x[i] = j; } } } for (int i = 0; i < N; i++){ cout << x[i] + 1; if (i < N - 1){ cout << ' '; } } cout << endl; } }