#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 5e5 + 100;
vector<pair<int, int>> node[N];
int dis[N][2], n, m;
void bfs() {
  memset(dis, inf, sizeof(dis));
  dis[n][0] = dis[n][1] = 0;
  queue<int> q;
  q.push(n);
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (auto t : node[u]) {
      int v = t.first;
      int type = t.second;
      int before = max(dis[v][0], dis[v][1]);
      dis[v][type] = min(dis[v][type], max(dis[u][0], dis[u][1]) + 1);
      int after = max(dis[v][0], dis[v][1]);
      if (after < before) q.push(v);
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  while (m--) {
    int u, v, c;
    scanf("%d%d%d", &u, &v, &c);
    node[v].emplace_back(u, c);
  }
  bfs();
  int ans = max(dis[1][0], dis[1][1]);
  if (ans == inf) ans = -1;
  printf("%d\n", ans);
  for (int i = 1; i <= n; i++)
    if (dis[i][0] >= dis[i][1])
      putchar('0');
    else
      putchar('1');
  return 0;
}
