#include <bits/stdc++.h>
using namespace std;
const long long N = 5e5 + 5;
long long n, m, vis[N], c[N], d[N];
vector<pair<long long, long long> > adj[N];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ;
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    long long u, v, t;
    cin >> u >> v >> t;
    adj[v].push_back({u, t});
  }
  memset(d, -1, sizeof(d));
  queue<long long> q;
  set<long long> st[2];
  q.push(n);
  vis[n] = 1;
  d[n] = 0;
  while (!q.empty()) {
    long long v = q.front();
    q.pop();
    for (pair<long long, long long> p : adj[v]) {
      long long w = p.first, t = p.second;
      if (vis[w]) continue;
      if (st[t].find(w) != st[t].end()) {
        vis[w] = 1;
        c[w] = t;
        st[t ^ 1].insert(w);
        q.push(w);
        d[w] = d[v] + 1;
      } else {
        st[t ^ 1].insert(w);
        c[w] = t ^ 1;
      }
    }
  }
  cout << d[1] << "\n";
  for (long long i = 1; i <= n; i++) cout << c[i];
}
