#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 100;
const int inf = 0x3f3f3f3f;
int n, m, b[maxn], w[maxn], d[maxn];
vector<int> bg[maxn], wg[maxn];
int main() {
  ios::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;
    if (t)
      wg[v].push_back(u);
    else
      bg[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i) d[i] = b[i] = w[i] = inf;
  d[n] = b[n] = w[n] = 0;
  queue<int> q;
  q.push(n);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < bg[u].size(); ++i) {
      int v = bg[u][i];
      if (b[v] != inf) continue;
      b[v] = d[u] + 1;
      if (b[v] != inf && w[v] != inf) {
        q.push(v);
        d[v] = max(b[v], w[v]);
      }
    }
    for (int i = 0; i < wg[u].size(); ++i) {
      int v = wg[u][i];
      if (w[v] != inf) continue;
      w[v] = d[u] + 1;
      if (b[v] != inf && w[v] != inf) {
        q.push(v);
        d[v] = max(b[v], w[v]);
      }
    }
  }
  if (d[1] == inf)
    cout << "-1\n";
  else
    cout << d[1] << '\n';
  for (int i = 1; i <= n; ++i) {
    if (w[i] >= b[i])
      cout << 1;
    else
      cout << 0;
  }
  cout << '\n';
}
