#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f;
const int N = 5e5 + 10;
const int M = 1e6 + 10;
const int mod = 1e9 + 7;
inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
inline long long read() {
  long long first = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    first = first * 10 + c - '0';
    c = getchar();
  }
  return first * f;
}
inline long long qpow(long long first, long long k = mod - 2,
                      long long m = mod) {
  long long res = 1;
  while (k) {
    if (k & 1) res = res * first % m;
    first = first * first % m;
    k >>= 1;
  }
  return res;
}
int n, m;
int head[N], to[N * 2], nxt[N * 2], val[N * 2], tot;
int dis[N], clr[N];
void add(int u, int v, int w) {
  to[++tot] = v;
  nxt[tot] = head[u];
  val[tot] = w;
  head[u] = tot;
}
queue<int> que;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, u, v, w; i <= m; i++) {
    scanf("%d%d%d", &u, &v, &w);
    add(v, u, w);
  }
  memset(dis, 0x3f, sizeof(dis));
  memset(clr, -1, sizeof(clr));
  dis[n] = 0;
  clr[n] = 0;
  que.push(n);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int i = head[u]; i; i = nxt[i]) {
      if (clr[to[i]] == -1) {
        clr[to[i]] = !val[i];
        continue;
      } else if (clr[to[i]] == val[i] && dis[to[i]] > dis[u] + 1) {
        dis[to[i]] = dis[u] + 1;
        que.push(to[i]);
      }
    }
  }
  printf("%d\n", dis[1] == inf ? -1 : dis[1]);
  for (int i = 1; i <= n; i++) printf("%d", max(clr[i], 0));
  return 0;
}
