#include <bits/stdc++.h>
using namespace std;
const int M = 5e5 + 10;
const int inf = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
const int bit = 32;
const double pi = acos(-1.0);
queue<int> q;
vector<pair<int, int> > v[M];
int a[M], co[M], d[M];
int main() {
  memset(d, inf, sizeof(d));
  memset(co, -1, sizeof(co));
  int n, m;
  scanf("%d%d", &n, &m);
  while (m--) {
    int x, y, t;
    scanf("%d%d%d", &x, &y, &t);
    v[y].push_back(make_pair(x, t));
  }
  d[n] = 0;
  co[n] = 0;
  q.push(n);
  while (q.size()) {
    int c = q.front();
    q.pop();
    for (int i = 0; i < v[c].size(); i++) {
      pair<int, int> p = v[c][i];
      int u = p.first, t = p.second;
      if (co[u] == -1) {
        co[u] = t ^ 1;
      } else {
        if (co[u] == t && d[u] > d[c] + 1) {
          d[u] = d[c] + 1;
          q.push(u);
        }
      }
    }
  }
  int ans = 0;
  if (d[1] == inf) {
    ans = -1;
  } else
    ans = d[1];
  printf("%d\n", ans);
  for (int i = 1; i <= n; i++) {
    printf("%d", max(co[i], 0));
  }
  puts("");
  return 0;
}
