#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int> > *go = new vector<pair<int, int> >[n + 1];
  while (m--) {
    int x, y, c;
    cin >> x >> y >> c;
    go[y].push_back({x, c});
  }
  int *color = new int[n + 1];
  int *f = new int[n + 1];
  fill(color, color + n + 1, -1);
  fill(f, f + n + 1, 0x7fffffff);
  deque<int> d;
  f[n] = 0;
  d.push_back(n);
  while (!d.empty()) {
    int o = d.front();
    d.pop_front();
    for (auto i : go[o]) {
      int t = i.first;
      int c = i.second;
      if (color[t] == -1)
        color[t] = 1 - c;
      else if (color[t] == c && f[t] > f[o] + 1) {
        f[t] = f[o] + 1;
        d.push_back(t);
      }
    }
  }
  if (f[1] == 0x7fffffff)
    cout << -1 << endl;
  else
    cout << f[1] << endl;
  for (int i = 1; i <= n; ++i)
    if (color[i] == -1)
      cout << 0;
    else
      cout << color[i];
  cout << endl;
  delete[] color;
  delete[] f;
  delete[] go;
  return 0;
}
