#include <bits/stdc++.h>
using namespace std;
const int MX = 5e5 + 69;
vector<pair<int, int> > adj[MX];
int black[MX], white[MX], dp[MX];
int n, m;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    adj[v].push_back(make_pair(u, w));
  }
  for (int i = 0; i < n; i++) {
    black[i] = MX;
    white[i] = MX;
    dp[i] = MX;
  }
  black[n] = 0;
  white[n] = 0;
  dp[n] = 0;
  deque<int> queue;
  queue.push_back(n);
  while (!queue.empty()) {
    int u = queue.front();
    queue.pop_front();
    for (pair<int, int> p : adj[u]) {
      int v = p.first;
      int w = p.second;
      if (dp[v] < MX) continue;
      if (w == 0)
        black[v] = min(black[v], 1 + dp[u]);
      else
        white[v] = min(white[v], 1 + dp[u]);
      dp[v] = max(black[v], white[v]);
      if (dp[v] < MX) queue.push_back(v);
    }
  }
  if (dp[1] == MX)
    cout << "-1\n";
  else
    cout << dp[1] << '\n';
  for (int i = 1; i <= n; i++) {
    cout << (black[i] > white[i] ? 0 : 1);
  }
  cout << '\n';
}
