#include <bits/stdc++.h>
using namespace std;
long long poww(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
const long long MAXN = 1e6 + 10;
const long long INF = 8e18;
const long long MOD = 1e9 + 7;
long long dp[MAXN], W[MAXN], B[MAXN], C[MAXN], n, m;
vector<long long> adj_w[MAXN], adj_b[MAXN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    long long u, v, c;
    cin >> u >> v >> c;
    swap(v, u);
    if (c)
      adj_b[u].push_back(v);
    else
      adj_w[u].push_back(v);
  }
  fill(C, C + MAXN, 2);
  fill(W, W + MAXN, n);
  fill(dp, dp + MAXN, n);
  fill(B, B + MAXN, n);
  dp[n] = W[n] = B[n] = 0;
  queue<long long> q;
  q.push(n);
  while (!q.empty()) {
    long long v = q.front();
    q.pop();
    for (long long u : adj_b[v]) {
      if (B[u] < n) continue;
      B[u] = dp[v] + 1;
      if (max(B[u], W[u]) < n) {
        q.push(u);
        dp[u] = max(B[u], W[u]);
      }
    }
    for (long long u : adj_w[v]) {
      if (W[u] < n) continue;
      W[u] = dp[v] + 1;
      if (max(B[u], W[u]) < n) {
        q.push(u);
        dp[u] = max(B[u], W[u]);
      }
    }
  }
  if (dp[1] >= n) dp[1] = -1;
  cout << dp[1] << '\n';
  for (int i = 1; i <= n; i++) {
    if (W[i] > B[i])
      cout << 0;
    else
      cout << 1;
  }
  cout << '\n';
  return 0;
}
