#include <bits/stdc++.h>
using namespace std;
int n;
const int N = 5e5 + 5;
vector<pair<int, int>> adj[N];
int bad[N], C[N], vis[N], d[N], m;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    if (a == b) continue;
    adj[b].push_back({a, w});
  }
  for (int i = 1; i < n; i++) C[i] = -1, d[i] = 100000000;
  d[n] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq;
  pq.push({0, n});
  C[n] = 0;
  while (!pq.empty()) {
    auto i1 = pq.top();
    pq.pop();
    int v = i1.second;
    int val = i1.first;
    if (vis[v]) continue;
    vis[v] = 1;
    if (v == 1) {
      cout << val << '\n';
      for (int i = 1; i <= n; i++)
        if (C[i] == -1)
          cout << 0;
        else
          cout << C[i];
      return 0;
    }
    for (auto i1 : adj[v]) {
      int i = i1.first;
      int x = i1.second;
      if (vis[i]) continue;
      if (d[i] > d[v] + 1) {
        if (C[i] == -1) {
          C[i] = (!x);
          continue;
        } else if (C[i] == x) {
          if (d[i] > val + 1) {
            d[i] = val + 1;
            pq.push({val + 1, i});
          }
        }
      }
    }
  }
  cout << "-1\n";
  for (int i = 1; i <= n; i++)
    if (C[i] != -1)
      cout << C[i];
    else
      cout << 1;
  return 0;
}
