#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void mine(T1 &x, T2 y) {
  if (x > y) x = y;
}
template <typename T1, typename T2>
inline void maxe(T1 &x, T2 y) {
  if (x < y) x = y;
}
ostream &operator<<(ostream &a, const vector<long long> &b) {
  for (auto k : b) cout << k << ' ';
  return a;
}
const long long MOD = 1000000007;
const long long INF = 1000000050;
const long long BIG = (long long)2e18 + 50;
const long long MX = 500010;
const double EPS = 1e-9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<pair<long long, long long> > g[MX];
long long col[MX];
long long dist[MX];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, m;
  cin >> n >> m;
  memset(col, -1, sizeof(col));
  fill(dist, dist + n, INF);
  for (long long i = 0; i < m; i++) {
    long long a, b, c;
    cin >> a >> b >> c;
    --a;
    --b;
    g[b].push_back({a, c});
  }
  col[n - 1] = dist[n - 1] = 0;
  queue<long long> q;
  q.push(n - 1);
  while (!q.empty()) {
    long long v = q.front();
    q.pop();
    for (auto [to, c] : g[v]) {
      if (dist[to] == INF) {
        if (col[to] == -1) {
          col[to] = c ^ 1;
        } else if (col[to] == c) {
          dist[to] = dist[v] + 1;
          q.push(to);
        }
      }
    }
  }
  cout << (dist[0] == INF ? -1 : dist[0]) << '\n';
  for (long long i = 0; i < n; i++) cout << max(0ll, col[i]);
  cout << '\n';
}
