#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
const int inf = 0x3f3f3f3f;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = 10 * x + ch - '0';
    ch = getchar();
  }
  return f * x;
}
vector<pair<int, int> > e[N];
int vis[N], ans[N], dis[N];
int main() {
  int T = 1;
  while (T--) {
    int n = read(), m = read();
    for (int i = 1; i <= m; i++) {
      int x = read(), y = read(), z = read();
      e[y].push_back(make_pair(x, z));
    }
    queue<int> q;
    memset(ans, -1, sizeof(ans));
    ans[n] = 0;
    dis[n] = 0;
    vis[n] = 1;
    q.push(n);
    while (!q.empty()) {
      int u = q.front();
      if (u == 1) break;
      q.pop();
      for (auto x : e[u]) {
        int v = x.first, w = x.second;
        if (vis[v]) continue;
        if (ans[v] != -1 && ans[v] == w) {
          q.push(v);
          vis[v] = 1;
          dis[v] = dis[u] + 1;
        } else
          ans[v] = w ^ 1;
      }
    }
    if (vis[1])
      printf("%d\n", dis[1]);
    else
      printf("-1\n");
    for (int i = 1; i <= n; i++)
      if (ans[i] == -1) ans[i] = 0;
    for (int i = 1; i <= n; i++) printf("%d", ans[i]);
    printf("\n");
  }
  return 0;
}
