#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
vector<pair<int, int> > vect[maxn];
int col[maxn], dp[maxn], n, m;
int go() {
  int x = n;
  queue<int> q;
  q.push(x);
  dp[x] = 0;
  col[x] = 0;
  while (q.size()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < vect[x].size(); i++) {
      int id = vect[x][i].first;
      int id2 = vect[x][i].second;
      if (col[id] == -1)
        col[id] = (id2 ^ 1);
      else if (col[id] == id2 && dp[id] == -1) {
        dp[id] = dp[x] + 1;
        q.push(id);
      }
    }
  }
  return dp[1];
}
int main() {
  scanf("%d %d", &n, &m);
  memset(dp, -1, sizeof(dp));
  memset(col, -1, sizeof(col));
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    vect[v].push_back({u, w});
  }
  printf("%d\n", go());
  for (int i = 1; i <= n; i++) {
    if (col[i] == -1) col[i] = 0;
    printf("%d", col[i]);
  }
  return 0;
}
