#include <bits/stdc++.h>
using namespace std;
const int N = (int)5e5 + 50, INF = (int)1e9;
struct edge {
  int to, c;
};
int n, m;
vector<edge> G[N];
int d[N][2], res[N];
set<pair<int, int> > S;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < (m); ++i) {
    int u, v, c;
    cin >> u >> v >> c;
    u--, v--;
    G[v].push_back({u, c});
  }
  for (int i = 0; i < n; i++) d[i][0] = d[i][1] = res[i] = INF;
  d[n - 1][0] = d[n - 1][1] = res[n - 1] = 0;
  queue<int> que;
  que.push(n - 1);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (auto& e : G[v]) {
      if (max(d[e.to][0], d[e.to][1]) > res[v] + 1) {
        d[e.to][e.c] = min(d[e.to][e.c], res[v] + 1);
        if (max(d[e.to][0], d[e.to][1]) == res[v] + 1) {
          res[e.to] = res[v] + 1;
          que.push(e.to);
        }
      }
    }
  }
  cout << (res[0] == INF ? -1 : res[0]) << '\n';
  for (int i = 0; i < (n); ++i) {
    cout << (d[i][1] > d[i][0]);
  }
  cout << '\n';
}
