#include <bits/stdc++.h>
using namespace std;
const int INF = 1e6;
int main() {
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<array<vector<int>, 2>> g(n + 1);
  for (int i = 0; i < m; i++) {
    int u, v, t;
    cin >> u >> v >> t;
    g[v][t].push_back(u);
  }
  vector<array<int, 2>> dist(n + 1, {INF, INF});
  vector<bool> fin(n + 1, false);
  vector<pair<int, int>> hp;
  dist[n] = {0, 0};
  hp.push_back({0, n});
  while (!hp.empty()) {
    int v = hp[0].second;
    pop_heap(hp.begin(), hp.end());
    hp.pop_back();
    if (!fin[v]) {
      fin[v] = true;
      int d = max(dist[v][0], dist[v][1]);
      for (int tp = 0; tp <= 1; tp++) {
        for (int w : g[v][tp]) {
          int pv = max(dist[w][0], dist[w][1]);
          dist[w][tp] = min(dist[w][tp], d + 1);
          int nv = max(dist[w][0], dist[w][1]);
          if (pv != nv) {
            hp.push_back({-nv, w});
            push_heap(hp.begin(), hp.end());
          }
        }
      }
    }
  }
  int res = max(dist[1][0], dist[1][1]);
  cout << ((res == INF) ? -1 : res) << endl;
  string s(n, '0');
  for (int i = 1; i <= n; i++) {
    s[i - 1] = '0' + (dist[i][1] >= dist[i][0]);
  }
  cout << s << endl;
  return 0;
}
