#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && (c != '-')) c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
const int N = 5e5 + 10, inf = 1e9;
int n, m, dis[N], ans[N];
vector<int> e[N][2];
priority_queue<pair<int, int> > q;
int main() {
  n = read(), m = read();
  for (register int i = (1); i <= (m); i++) {
    int x = read(), y = read(), z = read();
    e[y][z].push_back(x);
  }
  for (register int i = (1); i <= (n); i++) dis[i] = inf, ans[i] = -1;
  dis[n] = 0, q.push({0, n});
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    for (register int i = (0); i <= (1); i++) {
      for (auto v : e[u][i]) {
        if (ans[v] == -1)
          ans[v] = i ^ 1;
        else if (ans[v] == i) {
          if (dis[u] + 1 < dis[v]) {
            dis[v] = dis[u] + 1, q.push({-dis[v], v});
          }
        }
      }
    }
  }
  printf("%d\n", dis[1] == inf ? -1 : dis[1]);
  for (register int i = (1); i <= (n); i++) printf("%d", max(ans[i], 0));
}
