#include <bits/stdc++.h>
using namespace std;
struct IO {
  char buf[(1 << 20)], *p1, *p2;
  char pbuf[(1 << 20)], *pp;
  IO() : p1(buf), p2(buf), pp(pbuf) {}
  inline char gc() {
    return getchar();
    if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, (1 << 20), stdin);
    return p1 == p2 ? ' ' : *p1++;
  }
  inline bool blank(char ch) {
    return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
  }
  template <class T>
  inline void read(T &x) {
    register double tmp = 1;
    register bool sign = 0;
    x = 0;
    register char ch = gc();
    for (; !(ch >= '0' && ch <= '9'); ch = gc())
      if (ch == '-') sign = 1;
    for (; (ch >= '0' && ch <= '9'); ch = gc()) x = x * 10 + (ch - '0');
    if (ch == '.')
      for (ch = gc(); (ch >= '0' && ch <= '9'); ch = gc())
        tmp /= 10.0, x += tmp * (ch - '0');
    if (sign) x = -x;
  }
  inline void read(char *s) {
    register char ch = gc();
    for (; blank(ch); ch = gc())
      ;
    for (; !blank(ch); ch = gc()) *s++ = ch;
    *s = 0;
  }
  inline void read(char &c) {
    for (c = gc(); blank(c); c = gc())
      ;
  }
  template <class t>
  inline void write(t x) {
    if (x < 0)
      putchar('-'), write(-x);
    else {
      if (x > 9) write(x / 10);
      putchar('0' + x % 10);
    }
  }
} io;
const int mod = 1e9 + 7;
const int mo = 998244353;
const int N = 5e5 + 5;
int n, m, f[N][2], vis[N], head[N], cnt;
struct Node {
  int nex, to, w;
};
Node e[N << 1];
inline void jia(int u, int v, int w) {
  e[++cnt].nex = head[u];
  head[u] = cnt;
  e[cnt].to = v;
  e[cnt].w = w;
}
inline void spfa() {
  memset(f, 88, sizeof(f));
  memset(vis, 0, sizeof(vis));
  queue<int> q;
  q.push(n);
  f[n][1] = f[n][0] = 0;
  vis[n] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    int mx = max(f[u][1], f[u][0]);
    for (int i = head[u]; i; i = e[i].nex) {
      int v = e[i].to;
      int w = e[i].w;
      if (f[v][w] > mx + 1) {
        f[v][w] = mx + 1;
        if (!vis[v]) vis[v] = 1, q.push(v);
      }
    }
  }
}
int main() {
  io.read(n), io.read(m);
  for (int i = (1); i <= (m); i++) {
    int x, y, z;
    io.read(x), io.read(y), io.read(z);
    if (x == y) continue;
    jia(y, x, z);
  }
  spfa();
  int S = max(f[1][0], f[1][1]);
  if (S == f[0][0]) S = -1;
  io.write(S), puts("");
  for (int i = (1); i <= (n); i++) putchar((f[i][0] < f[i][1]) ? '1' : '0');
  return 0;
}
