#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5, inf = 0x7fffffff - 1;
int nxt[maxn], to[maxn], head[maxn], tp[maxn], pre[maxn][2], cl[maxn];
int n, m, cnt;
int dis[maxn][2];
bool inq[maxn];
void add(int x, int y, int k) {
  nxt[++cnt] = head[x];
  head[x] = cnt;
  to[cnt] = y;
  tp[cnt] = k;
}
queue<int> q;
void spfa() {
  int x, y;
  for (int i = 1; i <= n; i++) dis[i][0] = dis[i][1] = inf;
  q.push(n);
  dis[n][0] = dis[n][1] = 0;
  inq[n] = 1;
  while (!q.empty()) {
    x = q.front();
    q.pop();
    inq[x] = 0;
    for (int i = head[x]; i; i = nxt[i]) {
      y = to[i];
      int tmp = max(dis[x][0], dis[x][1]);
      if (dis[y][tp[i]] > tmp + 1) {
        dis[y][tp[i]] = tmp + 1;
        pre[y][tp[i]] = x;
        if (!inq[y]) q.push(y), inq[y] = 1;
      }
    }
  }
}
int main() {
  int x, y, k;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &x, &y, &k);
    add(y, x, k);
  }
  spfa();
  int ans = max(dis[1][0], dis[1][1]);
  if (ans == inf)
    printf("-1\n");
  else
    printf("%d\n", ans);
  for (int i = 1; i <= n; i++) {
    if (dis[i][0] > dis[i][1])
      printf("0");
    else
      printf("1");
  }
  return 0;
}
