#include <bits/stdc++.h>
using namespace std;
inline int max(int x, int y) { return x > y ? x : y; }
inline int min(int x, int y) { return x > y ? y : x; }
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
queue<int> q;
const int N = 500000 + 10;
int n, m, head[N], cnt, col[N], dis[N];
bool vis[N];
struct node {
  int to, next, opt;
} edge[N];
inline void add(int u, int v, int opt) {
  edge[++cnt] = {v, head[u], opt};
  head[u] = cnt;
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read(), opt = read();
    add(v, u, opt);
  }
  if (n == 1) return printf("0\n0"), 0;
  memset(col, -1, sizeof(col));
  q.push(n);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (u == 1) break;
    for (int i = head[u]; i; i = edge[i].next) {
      int v = edge[i].to;
      if (vis[v]) continue;
      if (col[v] == -1 || col[v] == 1 - edge[i].opt)
        col[v] = 1 - edge[i].opt;
      else {
        q.push(v);
        dis[v] = dis[u] + 1;
        vis[v] = 1;
      }
    }
  }
  if (dis[1])
    cout << dis[1] << endl;
  else
    cout << -1 << endl;
  for (int i = 1; i <= n; i++) {
    if (col[i] != -1)
      cout << col[i];
    else
      cout << 1;
  }
}
