#include <bits/stdc++.h>
using namespace std;
const int N = 5e5;
int n, m;
vector<pair<int, int>> rvs[N];
int color[N];
int dist[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int e = 0; e < m; e++) {
    int u, v, t;
    scanf("%d%d%d", &u, &v, &t);
    u--, v--;
    rvs[v].emplace_back(u, t);
  }
  fill_n(color, n, -1);
  fill_n(dist, n, -1);
  queue<int> q;
  q.push(n - 1);
  dist[n - 1] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto [v, t] : rvs[u]) {
      if (color[v] == -1)
        color[v] = 1 - t;
      else if (color[v] == t && dist[v] == -1) {
        dist[v] = dist[u] + 1;
        q.push(v);
      }
    }
  }
  printf("%d\n", dist[0]);
  for (int u = 0; u < n; u++) {
    if (color[u] == -1) color[u] = 0;
    printf("%d", color[u]);
  }
}
