#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long m) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
const long long sz = 5e5 + 5;
vector<pair<long long, long long> > adj[sz];
long long dis[sz];
long long col[sz];
void bfs(long long n) {
  queue<long long> qq;
  qq.push(n);
  fill_n(dis, sz, 1e17);
  dis[n] = 0;
  while (!qq.empty()) {
    long long pp = qq.front();
    qq.pop();
    for (auto i : adj[pp]) {
      if (dis[i.first] > dis[pp] + 1) {
        if (col[i.first] == -1) {
          col[i.first] = (i.second ^ 1);
        } else if (col[i.first] == i.second) {
          dis[i.first] = dis[pp] + 1;
          qq.push(i.first);
        }
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  long long x, y, z;
  for (long long i = 0; i < m; i++) {
    cin >> x >> y >> z;
    adj[y].push_back({x, z});
  }
  for (long long i = 1; i <= n; i++) col[i] = -1;
  bfs(n);
  if (dis[1] == 1e17)
    cout << -1 << '\n';
  else
    cout << dis[1] << '\n';
  for (long long i = 1; i <= n; i++) {
    if (col[i] == -1) col[i] = 0;
  }
  for (long long i = 1; i <= n; i++) cout << col[i];
}
