#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n, m;
int jar[500003][3];
vector<pair<int, int> > vec[500003];
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    pq;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v, c;
    scanf("%d %d %d", &u, &v, &c);
    vec[v].push_back({u, c});
  }
  for (int i = 1; i < n; i++) jar[i][0] = jar[i][1] = jar[i][2] = INF;
  pq.push({0, n});
  while (pq.size()) {
    pair<int, int> cur = pq.top();
    pq.pop();
    int u = cur.second;
    int mov = cur.first;
    if (mov > jar[u][2]) continue;
    for (pair<int, int> nxt : vec[u]) {
      int v1 = nxt.first;
      int v2 = nxt.second;
      if (jar[v1][v2] > mov + 1) {
        jar[v1][v2] = mov + 1;
        if (jar[v1][2] > max(jar[v1][0], jar[v1][1])) {
          jar[v1][2] = max(jar[v1][0], jar[v1][1]);
          pq.push({max(jar[v1][0], jar[v1][1]), v1});
        }
      }
    }
  }
  printf("%d\n", (jar[1][2] == INF) ? -1 : jar[1][2]);
  for (int i = 1; i <= n; i++) {
    if (jar[i][0] < jar[i][1])
      printf("1");
    else
      printf("0");
  }
  printf("\n");
}
