#include <bits/stdc++.h>
using namespace std;
inline int read() {
  register int x = 0, c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while ('0' <= c && c <= '9') x = 10 * x + (c ^ 48), c = getchar();
  return x;
}
const int INF = 0x3f3f3f3f;
const int N = 500005;
const int M = 500005;
int to[M], nxt[M], typ[M];
int head[N], ecnt = 2;
inline void AddEdge(const int u, const int v, const int w) {
  to[ecnt] = v, nxt[ecnt] = head[u], typ[ecnt] = w;
  head[u] = ecnt++;
}
int n, m, dt[N], col[N];
bool vis[N];
queue<int> Q;
int main() {
  n = read(), m = read();
  for (int i = 1, u, v, w; i <= m; i++) {
    u = read(), v = read(), w = read();
    AddEdge(v, u, w);
  }
  memset(col, -1, sizeof(col));
  memset(dt, 0x3f, sizeof(dt));
  col[n] = dt[n] = 0, Q.push(n);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = head[u], v; i; i = nxt[i])
      if (dt[v = to[i]] > dt[u] + 1) {
        if (col[v] == -1) col[v] = !typ[i];
        if (col[v] == typ[i]) {
          dt[v] = dt[u] + 1;
          if (!vis[v]) {
            Q.push(v);
            vis[v] = true;
          }
        }
      }
  }
  if (dt[1] >= INF)
    puts("-1");
  else
    printf("%d\n", dt[1]);
  for (int i = 1; i <= n; i++)
    if (col[i] == -1)
      putchar('1');
    else
      putchar(col[i] + '0');
  puts("");
  return 0;
}
