#include <bits/stdc++.h>
using namespace std;
inline int qr() {
  int f = 0, fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}
const int N = 5e5 + 10;
int head[N], ver[N << 1], Next[N << 1], edge[N << 1], tot;
int d[N], ans[N], n, m;
inline void add(int x, int y, int z) {
  ver[++tot] = y;
  Next[tot] = head[x];
  edge[tot] = z;
  head[x] = tot;
}
int main() {
  n = qr(), m = qr();
  while (m--) {
    int x = qr(), y = qr(), z = qr();
    add(y, x, z);
  }
  memset(d, 0x3f, sizeof(int) * n);
  memset(ans, -1, sizeof(int) * n);
  queue<int> q;
  q.push(n);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (register int i = head[x]; i; i = Next[i]) {
      int y = ver[i];
      if (ans[y] == -1) {
        ans[y] = edge[i] ^ 1;
        continue;
      }
      if (ans[y] == edge[i] && d[y] == 0x3f3f3f3f) d[y] = d[x] + 1, q.push(y);
    }
  }
  printf("%d\n", d[1] == 0x3f3f3f3f ? -1 : d[1]);
  for (register int i = 1; i <= n; ++i) printf("%d", ans[i] == -1 ? 0 : ans[i]);
  return 0;
}
