#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int f = 1, ans = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return f * ans;
}
const int MAXN = 500011;
vector<pair<int, int> > vec[MAXN];
int N, M, dis[MAXN][2], vis[MAXN], inf;
queue<int> que;
void spfa() {
  memset(dis, 127 / 3, sizeof(dis));
  inf = dis[0][0];
  dis[N][0] = dis[N][1] = 0;
  que.push(N);
  while (!que.empty()) {
    int xx = que.front();
    que.pop();
    vis[xx] = 0;
    int W = max(dis[xx][0], dis[xx][1]);
    for (auto pp : vec[xx]) {
      int v = pp.first, c = pp.second;
      if (dis[v][c] > W + 1) {
        dis[v][c] = W + 1;
        if (!vis[v]) que.push(v);
      }
    }
  }
  return;
}
int main() {
  N = read(), M = read();
  for (int i = 1; i <= M; i++) {
    int u = read(), v = read(), w = read();
    vec[v].push_back(make_pair(u, w));
  }
  spfa();
  int Ans = max(dis[1][0], dis[1][1]);
  if (Ans >= inf)
    printf("-1\n");
  else
    printf("%d\n", Ans);
  for (int i = 1; i <= N; i++) printf(dis[i][0] > dis[i][1] ? "0" : "1");
  printf("\n");
  return 0;
}
