#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 1009, M = 0;
int n, m, col[N], dis[N], vis[N], d[N];
int head[N], nxt[N], ver[N], edge[N], tot;
void add(int x, int y, int w) {
  ver[++tot] = y;
  nxt[tot] = head[x];
  head[x] = tot;
  edge[tot] = w;
}
queue<int> q;
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y, w;
    cin >> x >> y >> w;
    add(y, x, w);
  }
  if (n == 1) {
    printf("0\n0\n");
    return 0;
  }
  memset(col, -1, sizeof(col));
  q.push(n);
  vis[n] = 0;
  while (q.size()) {
    int x = q.front();
    q.pop();
    if (x == 1) break;
    for (int i = head[x]; i; i = nxt[i])
      if (!vis[ver[i]]) {
        if (col[ver[i]] == -1 || col[ver[i]] == 1 - edge[i]) {
          col[ver[i]] = 1 - edge[i];
        } else {
          q.push(ver[i]);
          d[ver[i]] = d[x] + 1;
          vis[ver[i]] = 1;
        }
      }
  }
  printf("%d\n", d[1] ? d[1] : -1);
  for (int i = 1; i <= n; i++) {
    if (col[i] == -1)
      printf("0");
    else
      printf("%d", col[i]);
  }
  printf("\n");
  return 0;
}
