#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long long N = 2e5 + 10, alp = 26;
const long double pi = 3.14159265358979;
void solve() {
  long long n, m;
  cin >> n >> m;
  vector<vector<long long> > g1(n), rg1(n), g2(n), rg2(n);
  for (long long i = 0; i < m; ++i) {
    long long u, v, t;
    cin >> u >> v >> t;
    --u;
    --v;
    if (t)
      g1[u].push_back(v), rg1[v].push_back(u);
    else
      g2[u].push_back(v), rg2[v].push_back(u);
  }
  vector<long long> ans(n, -1), len(n, -1);
  ans[n - 1] = 0;
  len[n - 1] = 0;
  queue<long long> q;
  q.push(n - 1);
  while (q.size()) {
    long long u = q.front();
    q.pop();
    set<long long> s1, s2;
    for (long long to : rg1[u])
      if (ans[to] == 1 && len[to] == -1) len[to] = len[u] + 1, q.push(to);
    for (long long to : rg2[u])
      if (ans[to] == 2 && len[to] == -1) len[to] = len[u] + 1, q.push(to);
    for (long long to : rg1[u])
      if (ans[to] == -1) s1.insert(to);
    for (long long to : rg2[u])
      if (ans[to] == -1) s2.insert(to);
    for (long long x : s1)
      if (!s2.count(x)) ans[x] = 2;
    for (long long x : s2)
      if (!s1.count(x)) ans[x] = 1;
    for (long long x : s1)
      if (s2.count(x) && len[x] == -1)
        ans[x] = 1, len[x] = len[u] + 1, q.push(x);
  }
  cout << len[0] << "\n";
  for (long long i = 0; i < n; ++i) cout << (ans[i] == 1 ? 1 : 0);
}
int32_t main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) solve();
}
