#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> bl[n], wh[n];
  int u, v, c;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> c;
    u--, v--;
    if (c) {
      wh[v].push_back(u);
    } else
      bl[v].push_back(u);
  }
  int b[n], w[n], d[n];
  for (int i = 0; i < n; i++) b[i] = w[i] = d[i] = n;
  b[n - 1] = w[n - 1] = d[n - 1] = 0;
  std::queue<int> q;
  q.push(n - 1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : bl[u]) {
      if (b[v] == n) {
        b[v] = d[u] + 1;
        if (max(b[v], w[v]) < n) {
          d[v] = max(b[v], w[v]);
          q.push(v);
        }
      }
    }
    for (auto v : wh[u]) {
      if (w[v] == n) {
        w[v] = d[u] + 1;
        if (max(b[v], w[v]) < n) {
          d[v] = max(b[v], w[v]);
          q.push(v);
        }
      }
    }
  }
  if (d[0] == n) {
    cout << -1 << endl;
  } else
    cout << d[0] << endl;
  for (int i = 0; i < n; i++) {
    if (b[i] > w[i])
      cout << 0;
    else
      cout << 1;
  }
  return 0;
}
