#include <bits/stdc++.h>
long long read() {
  register char ch = getchar();
  register long long ret = 0;
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) {
    ret = ret * 10 + ch - '0';
    ch = getchar();
  }
  return ret;
}
struct edge {
  int v, w, nxt;
} e[500005 << 1];
int tot, head[500005];
void addedge(int u, int v, int w) {
  e[++tot] = (edge){v, w, head[u]};
  head[u] = tot;
}
int q[500005], hh, tt, col[500005], vis[500005], dis[500005], n, m;
void bfs() {
  q[1] = n;
  hh = tt = 1;
  memset(dis, 0x3f, sizeof dis);
  dis[n] = 0;
  while (hh <= tt) {
    int u = q[hh++];
    for (int i = head[u], v = e[i].v; i; i = e[i].nxt, v = e[i].v) {
      if (col[v] == -1) {
        col[v] = !e[i].w;
      } else if (col[v] == e[i].w) {
        if (dis[u] + 1 < dis[v]) {
          dis[v] = std::min(dis[v], dis[u] + 1);
          q[++tt] = v;
        }
      }
    }
  }
  printf("%d\n", dis[1] == dis[0] ? -1 : dis[1]);
  for (int i = (1); i <= (n); ++i) printf("%d", col[i] == -1 ? 1 : col[i]);
}
int main() {
  n = read();
  m = read();
  memset(col, -1, sizeof col);
  while (m--) {
    int u = read(), v = read(), w = read();
    addedge(v, u, w);
  }
  bfs();
}
