#include <bits/stdc++.h>
using namespace std;
const int N = 1000009, INF = 1 << 30;
int n, m, head[N], cnt, dis[N], vis[N], color[N];
queue<int> q;
struct Edge {
  int nxt, to;
} g[N * 2];
void add(int from, int to) {
  g[++cnt].nxt = head[from];
  g[cnt].to = to;
  head[from] = cnt;
}
void init() {
  scanf("%d %d", &n, &m);
  for (int i = 1, x, y, z; i <= m; i++)
    scanf("%d %d %d", &x, &y, &z), add(y, x + n * z);
}
void work() {
  for (int i = 1; i <= 2 * n; i++) dis[i] = INF;
  dis[n] = dis[n + n] = 0;
  q.push(n), q.push(n + n);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int u = (x - 1) % n + 1;
    if (!vis[u]) {
      color[u] = x <= n;
      vis[u] = 1;
      continue;
    }
    for (int i = head[u]; i; i = g[i].nxt) {
      int v = g[i].to;
      if (dis[v] == INF) dis[v] = dis[x] + 1, q.push(v);
    }
  }
  int ans = max(dis[1], dis[1 + n]);
  if (ans == INF)
    printf("-1\n");
  else
    printf("%d\n", ans);
  for (int i = 1; i <= n; i++) printf("%d", color[i]);
  puts("");
}
int main() {
  init();
  work();
  return 0;
}
