#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3fffffff;
const int maxn = 1e6 + 10;
vector<int> bl[maxn], wh[maxn];
int w[maxn], b[maxn], dp[maxn];
int n, m;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v, t;
    cin >> u >> v >> t;
    u--;
    v--;
    if (t)
      wh[v].push_back(u);
    else
      bl[v].push_back(u);
  }
  for (int i = 0; i < n; ++i) {
    w[i] = b[i] = dp[i] = inf;
  }
  w[n - 1] = b[n - 1] = dp[n - 1] = 0;
  queue<int> q;
  q.push(n - 1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto to : wh[u]) {
      if (w[to] < inf) continue;
      w[to] = dp[u] + 1;
      if (max(w[to], b[to]) < inf) {
        q.push(to);
        dp[to] = max(w[to], b[to]);
      }
    }
    for (auto to : bl[u]) {
      if (b[to] < inf) continue;
      b[to] = dp[u] + 1;
      if (max(w[to], b[to]) < inf) {
        q.push(to);
        dp[to] = max(w[to], b[to]);
      }
    }
  }
  if (dp[0] == inf)
    cout << "-1" << endl;
  else
    cout << dp[0] << endl;
  for (int i = 0; i < n; ++i) {
    cout << (w[i] > b[i]);
  }
  cout << endl;
}
