#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
int n, m;
vector<vector<pair<int, int>>> adj;
vector<int> dist, color;
void bfs(int v) {
  queue<int> q;
  q.push(v);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto it : adj[u]) {
      int to = it.first;
      if (color[to] == -1) {
        color[to] = (it.second == 1) ? 0 : 1;
      } else {
        if (dist[to] == INF && color[to] == it.second) {
          dist[to] = dist[u] + 1;
          q.push(to);
        }
      }
    }
  }
}
int main() {
  cin >> n >> m;
  dist.assign(n, INF);
  adj.resize(n);
  color.assign(n, -1);
  dist[n - 1] = 0;
  for (int i = 0; i < m; ++i) {
    int u, v, t;
    cin >> u >> v >> t;
    u--;
    v--;
    adj[v].push_back({u, t});
  }
  bfs(n - 1);
  if (dist[0] == INF) {
    cout << -1 << '\n';
  } else {
    cout << dist[0] << '\n';
  }
  for (int i = 0; i < n; ++i) {
    if (color[i] == -1)
      cout << 0;
    else
      cout << color[i];
  }
  return 0;
}
