#include <algorithm>
#include <iostream>
#include <vector>

#define let auto
#define fn auto
#define usize size_t
using namespace std;

#define UD 1'000'000

fn fastio() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

fn main() -> int {
    fastio();
    usize n, m;
    cin >> n >> m;

    let arr = new usize[5 * m];

    let music = new usize[n];
    let cnts = new usize[n];
    for (usize i = 0; i < n; i++) {
        music[i] = UD;
        cnts[i] = 0;
    }

    for (usize i = 0; i < m; i++) {
        for (usize j = 0; j < 4; j++) {
            cin >> arr[5 * i + j];// idx
            arr[5 * i + j] -= 1;
            cnts[arr[5 * i + j]]++;
        }
        cin >> arr[5 * i + 4];
    }

    let cntv = vector<pair<usize, usize>>();
    for (usize i = 0; i < n; i++) {
        cntv.push_back(pair(cnts[i], i));
    }

    sort(cntv.begin(), cntv.end());
    reverse(cntv.begin(), cntv.end());

    usize tot = 0;
    usize before = 0;
    for (let it = cntv.begin(); it < cntv.end(); it++) {
        if (tot > 3 * m) {
            music[before] = UD;
            break;
        } else if (tot == 3 * m) {
            break;
        }
        else {
            tot += it->first;
            music[it->second] = 0;
            before = it->second;
        }
    }

    for (usize i = 0; i < m; i++) {
        let x = arr[5 * i + 0];
        let y = arr[5 * i + 1];
        let z = arr[5 * i + 2];
        let w = arr[5 * i + 3];
        let k = arr[5 * i + 4];
        let mx = music[x];
        let my = music[y];
        let mz = music[z];
        let mw = music[w];
        let key = (mx % 4 + my % 4 + mz % 4 + mw % 4) % 4;
        if ((mx + my + mz + mw) / UD == 1) {
            if (mx == UD) {
                music[x] = (k + 4 - key) % 4;
            } else if (my == UD) {
                music[y] = (k + 4 - key) % 4;
            } else if (mz == UD) {
                music[z] = (k + 4 - key) % 4;
            } else {
                music[w] = (k + 4 - key) % 4;
            }
        }
    }

    for (usize i = 0; i < n; i++) {
        if (music[i] == UD) {
            music[i] = 0;
        }
    }

    for (usize i = 0; i < n; i++) {
        cout << music[i] << " ";
    }

    delete[] arr;
    delete[] music;
    delete[] cnts;

    return 0;
}