#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    --x;
    --y;
    ++z;
    g[y].emplace_back(x, z);
  }
  vector<int> dist(n, -1);
  vector<int> seen(n, 0);
  vector<bool> col(n);
  vector<int> que;
  dist[n - 1] = 0;
  que.push_back(n - 1);
  for (int i = 0; i < (int)que.size(); i++) {
    int v = que[i];
    for (auto ed : g[v]) {
      int x = ed.first;
      int c = ed.second;
      seen[x] |= c;
      if (seen[x] != 3) {
        col[x] = 2 - c;
      } else if (dist[x] == -1) {
        dist[x] = dist[v] + 1;
        col[x] = c - 1;
        que.push_back(x);
      }
    }
  }
  cout << dist[0] << '\n';
  for (int i = 0; i < n; i++) {
    cout << col[i];
  }
  cout << '\n';
  return 0;
}
