#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
const int inf = 0x3f3f3f3f;
int n, m;
vector<int> g[2][N];
bool vis[N];
int f[N][2];
pair<int, int> pre[N][2];
int col[N];
int main() {
  scanf("%d%d", &n, &m);
  int u, v, w;
  for (int i = (1); i < (m + 1); i++) {
    scanf("%d%d%d", &u, &v, &w);
    g[w][v].push_back(u);
  }
  memset(f, 0x3f, sizeof(f));
  queue<int> q;
  q.push(n);
  f[n][0] = f[n][1] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    int c = 0;
    vis[u] = 0;
    if (f[u][0] < f[u][1]) c = 1;
    for (auto v : g[0][u]) {
      if (max(f[u][0], f[u][1]) + 1 < f[v][0]) {
        f[v][0] = max(f[u][0], f[u][1]) + 1;
        pre[v][0] = pair<int, int>(u, c);
        if (!vis[v]) q.push(v), vis[v] = 1;
      }
    }
    for (auto v : g[1][u]) {
      if (max(f[u][0], f[u][1]) + 1 < f[v][1]) {
        f[v][1] = max(f[u][0], f[u][1]) + 1;
        pre[v][1] = pair<int, int>(u, c);
        if (!vis[v]) q.push(v), vis[v] = 1;
      }
    }
  }
  int ans = max(f[1][0], f[1][1]);
  if (ans == inf) ans = -1;
  printf("%d\n", ans);
  for (int i = (1); i < (n + 1); i++) {
    if (f[i][0] > f[i][1])
      printf("0");
    else
      printf("1");
  }
  return 0;
}
