#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int b[500005], w[500005], dp[500005];
vector<int> be[500005], we[500005];
queue<int> q;
int main() {
  int n, m, u, v, t;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) b[i] = w[i] = dp[i] = inf;
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &u, &v, &t);
    if (u == v) continue;
    if (t == 0)
      be[v].push_back(u);
    else
      we[v].push_back(u);
  }
  q.push(n);
  b[n] = w[n] = dp[n] = 0;
  while (!q.empty()) {
    int s = q.front();
    q.pop();
    for (int i = 0; i < be[s].size(); i++) {
      int y = be[s][i];
      if (b[y] != inf) continue;
      b[y] = dp[s] + 1;
      if (max(b[y], w[y]) != inf) {
        q.push(y);
        dp[y] = max(b[y], w[y]);
      }
    }
    for (int i = 0; i < we[s].size(); i++) {
      int y = we[s][i];
      if (w[y] != inf) continue;
      w[y] = dp[s] + 1;
      if (max(b[y], w[y]) != inf) {
        q.push(y);
        dp[y] = max(b[y], w[y]);
      }
    }
  }
  if (dp[1] == inf)
    printf("-1\n");
  else
    printf("%d\n", dp[1]);
  for (int i = 1; i <= n; i++) {
    if (b[i] < w[i])
      printf("1");
    else
      printf("0");
  }
  printf("\n");
}
