#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> edges(n);
  for (int i = 0; i < m; ++i) {
    int u, v, t;
    cin >> u >> v >> t;
    edges[v - 1].push_back({u - 1, t});
  }
  vector<int> b(n, INF), w(n, INF), dp(n, INF);
  queue<int> q;
  vector<bool> vis(n, false);
  b[n - 1] = w[n - 1] = dp[n - 1] = 0;
  q.push(n - 1);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto edge : edges[u]) {
      const int &v = edge.first, &type = edge.second;
      if (type == 1)
        w[v] = min(w[v], dp[u] + 1);
      else
        b[v] = min(b[v], dp[u] + 1);
      dp[v] = max(b[v], w[v]);
      if (dp[v] < INF && !vis[v]) {
        vis[v] = true;
        q.push(v);
      }
    }
  }
  cout << (dp[0] < INF ? dp[0] : -1) << endl;
  for (int i = 0; i < n; ++i) cout << (dp[i] == w[i] ? 1 : 0);
  cout << endl;
  return 0;
}
