#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
template <class t>
inline t read(t &x) {
  char c = getchar();
  bool f = 0;
  x = 0;
  while (!isdigit(c)) f |= c == '-', c = getchar();
  while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f) x = -x;
  return x;
}
template <class t, class... A>
inline void read(t &x, A &...a) {
  read(x);
  read(a...);
}
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);
  }
}
const int N = 5e5 + 5;
vector<pair<int, int> > g[N];
int n, m, col[N], dis[N];
queue<int> q;
signed main() {
  read(n, m);
  for (int i = 1, first, y, z; i <= m; i++) {
    read(first, y, z);
    g[y].emplace_back(first, z);
  }
  for (int i = 1; i <= n; i++) col[i] = -1, dis[i] = n + 1;
  q.push(n);
  dis[n] = 0;
  while (!q.empty()) {
    int first = q.front();
    q.pop();
    for (auto e : g[first]) {
      if (col[e.first] == -1)
        col[e.first] = !e.second;
      else if (col[e.first] == e.second && dis[first] + 1 < dis[e.first])
        dis[e.first] = dis[first] + 1, q.push(e.first);
    }
  }
  if (dis[1] == n + 1) dis[1] = -1;
  write(dis[1]);
  puts("");
  for (int i = 1; i <= n; i++) write(max(col[i], 0));
}
