#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
int c[N], d[N];
vector<pair<int, int>> G[N];
queue<int> q;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v, t;
    scanf("%d%d%d", &u, &v, &t);
    G[v].push_back({u, t});
  }
  for (int i = 1; i <= n; i++) c[i] = d[i] = -1;
  d[n] = 0;
  q.push(n);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto p : G[u]) {
      int v = p.first, t = p.second;
      if (c[v] < 0)
        c[v] = t ^ 1;
      else if (c[v] == t && d[v] < 0) {
        d[v] = d[u] + 1;
        q.push(v);
      }
    }
  }
  printf("%d\n", d[1]);
  for (int i = 1; i <= n; i++) {
    if (c[i] < 0) c[i] = 0;
    printf("%d", c[i]);
  }
  printf("\n");
  return 0;
}
