#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10, M = 5e5 + 10, oo = 0x3f3f3f3f;
vector<pair<int, int> > g[N];
int dis[N][2], vis[N];
int n, m;
void adde(int u, int v, int op) { g[u].push_back(make_pair(v, op)); }
void dijkstra(int s) {
  for (int i = 1; i <= n; i++) dis[i][0] = dis[i][1] = oo, vis[i] = 0;
  static priority_queue<pair<int, int> > pq;
  dis[s][0] = dis[s][1] = 0;
  pq.push(make_pair(0, s));
  for (; !pq.empty();) {
    int u = pq.top().second;
    pq.pop();
    if (vis[u]) continue;
    int d = max(dis[u][0], dis[u][1]);
    vis[u] = 1;
    for (pair<int, int> &e : g[u]) {
      int v = e.first, o = e.second;
      if (dis[v][o] > d + 1) {
        dis[v][o] = d + 1;
        pq.push(make_pair(-max(dis[v][0], dis[v][1]), v));
      }
    }
  }
  return;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, u, v, t; i <= m; i++) {
    scanf("%d%d%d", &u, &v, &t);
    adde(v, u, t);
  }
  dijkstra(n);
  int res = max(dis[1][0], dis[1][1]);
  if (res >= oo)
    printf("-1\n");
  else
    printf("%d\n", res);
  for (int i = 1; i <= n; i++) printf("%d", (dis[i][0] < dis[i][1]));
  printf("\n");
  return 0;
}
