#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

int n, m, adj[20];
int dp[262144];

void solve(int, int, int, int);

int get(int x) {
    if (!x) return 0;
    if (dp[x] != 2e9) return dp[x];
    solve(0, x, 0, x);
    return dp[x];
}

void solve(int p, int r, int s, int d) {
    if (!r && !s) {
        dp[d] = min(dp[d], 1 + get(d & ~p));
        return;
    }
    for (int i = n - 1; i >= 0; i--) if (r & 1 << i) {
        solve(p | 1 << i, r & adj[i], s & adj[i], d);
        r &= ~(1 << i);
        s |= 1 << i;
    }
}

int main() {
    scanf("%d%d", &n, &m);
    fill(dp, dp + 262144, 2e9);
    while (m--) {
        int a, b;
        scanf("%d%d", &a, &b);
        a--, b--;
        adj[a] |= 1 << b;
        adj[b] |= 1 << a;
    }
    solve(0, (1 << n) - 1, 0, (1 << n) - 1);
    printf("%d", dp[(1 << n) - 1]);
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>; 
using pll = pair<long long, long long>;
using vi = vector<int>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
constexpr long long INF = 1e9+10;
constexpr long long LINF = 1e18+10;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

struct edge{
    int from;
    int to;
    int c;
};

int main(){
    int n, m; cin >> n >> m;
    vector<vector<edge>> G(n+10);
    rep(i, m){
        int u, v, c; cin >> u >> v >> c;
        edge e1{u,v,c};
        edge e2{v,u,c};
        G[u].push_back(e1);
        G[v].push_back(e2);
    }
    //始点＝頂点0としてBFS
    vector<int> ans(n+10, INF);//頂点に振る番号
    queue<pii> que; //頂点番号,ラベル
    que.push({1, INF});//-1
    while(!que.empty()){
        auto p = que.front(); que.pop();
        int v = p.first, c = p.second; //一致なら正、不一致なら負
        //cout << "v:" << v << " c:" << c << ln;
        if(ans[v]!=INF)continue;
        if(c==INF)ans[v] = 1;
        else{
            if(c>0){
                ans[v] = (c == 1 ? 2 : 1);
            }else{
                c = -c;
                ans[v] = c;
            }     
        }
        for(auto e: G[v]){
            if(ans[e.to]!=INF)continue;
            int nc = e.c;
            if(ans[v]!=nc) nc = -e.c;
            que.push({e.to, nc});
        }
    }

    for(int i=1; i<=n; i++){
        cout << ans[i] << ln;
    }

}
