#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) { cerr << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0},  {-1, 0},
                       {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
const int maxn = 1e6 + 10;
int n, m;
vector<int> bg[maxn], rg[maxn];
int b[maxn], r[maxn], d[maxn], col[maxn];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = int(0); i < int(m); i++) {
    int u, v, t;
    scanf("%d%d%d", &u, &v, &t);
    --u, --v;
    if (!t)
      bg[v].push_back(u);
    else
      rg[v].push_back(u);
  }
  for (int i = int(0); i < int(n); i++) b[i] = r[i] = d[i] = n;
  queue<int> q;
  q.push(n - 1);
  b[n - 1] = r[n - 1] = d[n - 1] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (auto to : bg[x]) {
      if (b[to] < n) continue;
      b[to] = d[x] + 1;
      if (max(b[to], r[to]) < n) {
        q.push(to);
        d[to] = max(b[to], r[to]);
      }
    }
    for (auto to : rg[x]) {
      if (r[to] < n) continue;
      r[to] = d[x] + 1;
      if (max(b[to], r[to]) < n) {
        q.push(to);
        d[to] = max(b[to], r[to]);
      }
    }
  }
  if (d[0] == n)
    printf("-1\n");
  else
    printf("%d\n", d[0]);
  for (int i = int(0); i < int(n); i++) {
    if (b[i] > r[i])
      col[i] = 0;
    else
      col[i] = 1;
  }
  for (int i = int(0); i < int(n); i++) {
    printf("%d", col[i]);
  }
  printf("\n");
  return 0;
}
