#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 100;
vector<pair<int, int>> adj[N];
int color[N], dist[N], d[N][2];
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, t;
    cin >> u >> v >> t;
    adj[v].push_back(make_pair(u, t));
  }
  memset(color, -1, sizeof color);
  memset(dist, 0x3f, sizeof dist);
  memset(d, 0x3f, sizeof d);
  color[n] = dist[n] = d[n][0] = d[n][1] = 0;
  set<pair<int, int>> G;
  G.insert(make_pair(0, n));
  while (G.size()) {
    auto [dv, v] = *G.begin();
    G.erase(G.begin());
    for (const auto &[u, t] : adj[v]) {
      if (color[u] < 0) color[u] = t ^ 1;
      d[u][t] = min(d[u][t], dist[v] + 1);
    }
    for (const auto &[u, t] : adj[v])
      if (dist[u] > d[u][color[u]]) {
        G.erase(make_pair(dist[u], u));
        dist[u] = d[u][color[u]];
        G.insert(make_pair(dist[u], u));
      }
  }
  cout << (dist[1] >= dist[0] ? -1 : dist[1]) << "\n";
  for (int i = 1; i <= n; i++) {
    if (color[i] < 0) color[i] = 0;
    cout << color[i];
  }
  cout << "\n";
}
