#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
using namespace std;
namespace ywy {
inline int get() {
  int n = 0;
  char c;
  while ((c = getchar()) || 23333)
    if (c >= '0' && c <= '9') break;
  n = c - '0';
  while ((c = getchar()) || 23333) {
    if (c >= '0' && c <= '9')
      n = n * 10 + c - '0';
    else
      return (n);
  }
}
typedef struct _b {
  int dest;
  int nxt;
  int col;
} bian;
bian memchi[500010];
int heads[500010], dis[500010], gn = 1, col[500010], que[500010];
inline void add(int s, int t, int c) {
  memchi[gn].dest = t;
  memchi[gn].col = c;
  memchi[gn].nxt = heads[s];
  heads[s] = gn;
  gn++;
}
void ywymain() {
  memset(col, -1, sizeof(col));
  memset(dis, -1, sizeof(dis));
  int n = get(), m = get();
  for (register int i = 1; i <= m; i++) {
    int t = get(), s = get(), c = get();
    add(s, t, c);
  }
  col[n] = 0;
  dis[n] = 0;
  int head = 0, tail = 1;
  que[0] = n;
  while (head < tail) {
    int me = que[head];
    head++;
    for (register int i = heads[me]; i; i = memchi[i].nxt) {
      if (dis[memchi[i].dest] != -1) continue;
      if (col[memchi[i].dest] == -1) {
        col[memchi[i].dest] = memchi[i].col ^ 1;
        continue;
      }
      if (col[memchi[i].dest] != memchi[i].col) continue;
      col[memchi[i].dest] = memchi[i].col;
      dis[memchi[i].dest] = dis[me] + 1;
      que[tail] = memchi[i].dest;
      tail++;
    }
  }
  if (dis[1] == -1)
    printf("-1\n");
  else
    printf("%d\n", dis[1]);
  for (register int i = 1; i <= n; i++) {
    if (col[i] == -1)
      printf("0");
    else
      printf("%d", col[i]);
  }
}
}  // namespace ywy
int main() {
  ywy::ywymain();
  return (0);
}
