#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int n, m, col[maxn], dis[maxn];
int vis[maxn];
vector<pair<int, int> > g[maxn];
signed main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v, t;
    scanf("%d%d%d", &u, &v, &t);
    g[v].push_back({u, t});
  }
  memset(col, -1, sizeof(col));
  memset(dis, 127, sizeof(dis));
  dis[n] = 0;
  queue<int> q;
  q.push(n);
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (auto to : g[u]) {
      int v = to.first, t = to.second;
      if (col[v] == -1)
        col[v] = !t;
      else if (col[v] == t && dis[v] > dis[u] + 1)
        dis[v] = dis[u] + 1, q.push(v);
    }
  }
  printf("%d\n", dis[1] < 1e9 ? dis[1] : -1);
  for (int i = 1; i <= n; i++) printf("%d", max(col[i], 0));
}
