#include <bits/stdc++.h>
namespace IO {
template <typename T>
inline void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  x *= f;
  return;
}
template <typename T>
inline void write(T x = 0) {
  T wr[51];
  wr[0] = 0;
  if (x < 0) putchar('-'), x = -x;
  if (!x) putchar(48);
  while (x) wr[++wr[0]] = x % 10, x /= 10;
  while (wr[0]) putchar(48 + wr[wr[0]--]);
  return;
}
}  // namespace IO
using IO::read;
using IO::write;
using namespace std;
const int N = 1e6 + 11;
int n, m, tot, x, y, z;
int nxt[N], las[N], to[N], w[N], col[N], dis[N];
void add(int x, int y, int z) {
  nxt[++tot] = las[x];
  las[x] = tot;
  to[tot] = y;
  w[tot] = z;
}
queue<int> q;
void bfs() {
  for (register int i = 1; i <= n; ++i) col[i] = -1;
  col[n] = 0, dis[n] = 1;
  q.push(n);
  while (!q.empty()) {
    x = q.front(), q.pop();
    for (int e = las[x]; e; e = nxt[e]) {
      if (col[to[e]] == -1)
        col[to[e]] = w[e] ^ 1;
      else {
        if (col[to[e]] == w[e] && !dis[to[e]]) {
          dis[to[e]] = dis[x] + 1;
          q.push(to[e]);
        }
      }
    }
  }
}
int main() {
  read(n), read(m);
  for (register int i = 1; i <= m; ++i) {
    read(x), read(y), read(z);
    add(y, x, z);
  }
  bfs();
  if (col[1] == -1) {
    printf("-1\n");
    for (register int i = 1; i <= n; ++i)
      if (col[i] == -1)
        printf("0");
      else
        printf("%d", col[i]);
  } else {
    printf("%d\n", dis[1] - 1);
    for (register int i = 1; i <= n; ++i)
      if (col[i] == -1)
        printf("0");
      else
        printf("%d", col[i]);
  }
  return 0;
}
