#include <bits/stdc++.h>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const long long maxn = (long long)1e6 + 5;
const long long mod = 998244353;
const long long inf = 0x3f3f3f3f;
long long T = 1;
bool vis[maxn];
long long col[maxn], d[maxn];
vector<pair<long long, long long> > v[maxn];
void solve() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i <= m; ++i) {
    long long x, y, z;
    cin >> x >> y >> z;
    v[y].push_back({x, z});
  }
  if (n == 1) {
    cout << "0\n0";
    return;
  }
  queue<long long> q;
  q.push(n);
  vis[n] = false;
  memset(col, -1, sizeof(col));
  for (long long i = 1; i <= n; ++i) reverse(v[i].begin(), v[i].end());
  while (!q.empty()) {
    long long x = q.front();
    q.pop();
    if (x == 1) break;
    for (auto &to : v[x]) {
      long long v = to.first;
      if (vis[v]) continue;
      if (col[v] == -1 || col[v] == 1 - to.second)
        col[v] = 1 - to.second;
      else {
        q.push(v);
        d[v] = d[x] + 1;
        vis[v] = true;
      }
    }
  }
  if (d[1])
    cout << d[1] << '\n';
  else
    cout << -1 << '\n';
  for (long long i = 1; i <= n; ++i) {
    if (col[i] == -1)
      cout << 0;
    else
      cout << col[i];
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  while (T--) solve();
  return 0;
}
