#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5, M = 5e5 + 5;
vector<pair<int, int> > e[N];
const int INF = 1e9;
int b[N], w[N], d[N], c[N];
int main(int argc, const char* argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    e[v].push_back({u, w});
  }
  for (int i = 1; i < n; i++) {
    d[i] = w[i] = b[i] = INF;
  }
  d[n] = b[n] = w[n] = 0;
  queue<int> q;
  q.push(n);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (pair<int, int> p : e[x]) {
      int to = p.first, ty = p.second;
      if (p.second) {
        if (w[to] == INF) {
          w[to] = d[x] + 1;
          if (max(w[to], b[to]) < INF) {
            q.push(to);
            d[to] = max(b[to], w[to]);
          }
        }
      } else {
        if (b[to] == INF) {
          b[to] = d[x] + 1;
          if (max(w[to], b[to]) < INF) {
            q.push(to);
            d[to] = max(b[to], w[to]);
          }
        }
      }
    }
  }
  if (d[1] == INF)
    cout << "-1\n";
  else
    cout << d[1] << '\n';
  for (int i = 1; i <= n; i++) {
    if (b[i] > w[i])
      cout << '0';
    else
      cout << '1';
  }
  cout << endl;
  return 0;
}
