#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
const long long N = 500010;
inline long long read() {
  long long s = 0, w = 1;
  register char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
  return s * w;
}
long long n, m, c[N], dis[N], book[N];
long long head[N], maxE;
struct Edge {
  long long nxt, to, rdis;
} e[N << 1];
inline void Add(long long u, long long v, long long w) {
  e[++maxE].nxt = head[u];
  head[u] = maxE;
  e[maxE].to = v;
  e[maxE].rdis = w;
}
struct Node {
  long long x, w;
  inline bool operator<(const Node& a) const { return w > a.w; }
};
priority_queue<Node> Q;
signed main() {
  n = read(), m = read();
  for (register long long i = 1; i <= m; i++) {
    long long u, v, w;
    u = read(), v = read(), w = read();
    Add(v, u, w);
  }
  memset(dis, 0x3f, sizeof(dis));
  dis[n] = 0;
  memset(c, -1, sizeof(c));
  c[n] = 0, Q.push((Node){n, 0});
  while (!Q.empty()) {
    Node now = Q.top();
    Q.pop();
    long long x = now.x;
    if (book[x]) continue;
    book[x] = 1;
    for (register long long i = head[x]; i; i = e[i].nxt) {
      long long v = e[i].to;
      if (c[v] == -1 || c[v] != e[i].rdis)
        c[v] = (!e[i].rdis);
      else {
        if (dis[v] > dis[x] + 1) {
          dis[v] = dis[x] + 1;
          Q.push((Node){v, dis[v]});
        }
      }
    }
  }
  if (dis[1] >= 1e18)
    puts("-1");
  else
    printf("%lld\n", dis[1]);
  for (register long long i = 1; i <= n; i++)
    printf("%lld", (~c[i]) ? (c[i]) : (0));
  puts("");
  return 0;
}
