#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
int n, m, dis[N], ans[N], vis[N], q[N], head, tail;
vector<int> vt[N][2];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int u, v, tp;
    scanf("%d%d%d", &u, &v, &tp);
    vt[v][tp].push_back(u);
  }
  for (int i = 1; i <= n; ++i) dis[i] = n + 1, ans[i] = -1;
  q[head = tail = 0] = n, vis[n] = 1, dis[n] = ans[n] = 0;
  while (head <= tail) {
    int x = q[head++];
    for (int i = 0; i <= 1; ++i) {
      int sz = vt[x][i].size();
      for (int j = 0; j <= sz - 1; ++j) {
        int v = vt[x][i][j];
        if (vis[v]) continue;
        if (ans[v] == -1)
          ans[v] = i ^ 1;
        else if (ans[v] == i) {
          dis[v] = dis[x] + 1;
          q[++tail] = v, vis[v] = 1;
        }
      }
    }
  }
  if (dis[1] == n + 1)
    puts("-1");
  else
    printf("%d\n", dis[1]);
  for (int i = 1; i <= n; ++i)
    if (ans[i] == -1) ans[i] = 0;
  for (int i = 1; i <= n; ++i) putchar('0' + ans[i]);
  puts("");
  return 0;
}
