#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxm = 5e5 + 10;
const int inf = 0x3f3f3f3f;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
int f, qr;
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
inline void putc(char x) {
  *oS++ = x;
  if (oS == oT) flush();
}
template <typename A>
inline bool read(A &x) {
  for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                              (iS == iT ? EOF : *iS++))
                            : *iS++);
       c < '0' || c > '9';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    if (c == '-')
      f = -1;
    else if (c == EOF)
      return 0;
  for (x = 0; c <= '9' && c >= '0';
       c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                       (iS == iT ? EOF : *iS++))
                     : *iS++))
    x = x * 10 + (c & 15);
  x *= f;
  return 1;
}
inline bool read(char &x) {
  while ((x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                           (iS == iT ? EOF : *iS++))
                        : *iS++)) == ' ' ||
         x == '\n' || x == '\r')
    ;
  return x != EOF;
}
inline bool read(char *x) {
  while ((*x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                            (iS == iT ? EOF : *iS++))
                         : *iS++)) == '\n' ||
         *x == ' ' || *x == '\r')
    ;
  if (*x == EOF) return 0;
  while (!(*x == '\n' || *x == ' ' || *x == '\r' || *x == EOF))
    *(++x) = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),
                          (iS == iT ? EOF : *iS++))
                       : *iS++);
  *x = 0;
  return 1;
}
template <typename A, typename... B>
inline bool read(A &x, B &...y) {
  return read(x) && read(y...);
}
template <typename A>
inline bool write(A x) {
  if (!x) putc('0');
  if (x < 0) putc('-'), x = -x;
  while (x) qu[++qr] = x % 10 + '0', x /= 10;
  while (qr) putc(qu[qr--]);
  return 0;
}
inline bool write(char x) {
  putc(x);
  return 0;
}
inline bool write(const char *x) {
  while (*x) {
    putc(*x);
    ++x;
  }
  return 0;
}
inline bool write(char *x) {
  while (*x) {
    putc(*x);
    ++x;
  }
  return 0;
}
template <typename A, typename... B>
inline bool write(A x, B... y) {
  return write(x) || write(y...);
}
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
}  // namespace io
using io ::putc;
using io ::read;
using io ::write;
int dis[maxm], inq[maxm], col[maxm], flag[maxm][2];
queue<int> q;
struct Edge {
  int v, col;
};
vector<Edge> ed[maxm];
int n;
void bfs() {
  while (!q.empty()) q.pop();
  q.push(n);
  inq[n] = 1;
  flag[n][0] = flag[n][1] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    dis[u] = max(flag[u][0], flag[u][1]);
    if (flag[u][0] < flag[u][1])
      col[u] = 1;
    else
      col[u] = 0;
    for (Edge nx : ed[u]) {
      int v = nx.v, cc = nx.col;
      flag[v][cc] = min(flag[v][cc], min(inf, dis[u] + 1));
      if ((!inq[v]) && flag[v][0] < inf && flag[v][1] < inf) {
        q.push(v);
        inq[v] = 1;
      }
    }
  }
}
int main() {
  int m;
  read(n, m);
  for (int i = 0; i <= n + 5; i++) {
    flag[i][0] = flag[i][1] = inf;
  }
  for (int i = 1; i <= m; i++) {
    int u, v, col;
    read(u, v, col);
    ed[v].push_back({u, col});
  }
  bfs();
  for (int i = 1; i <= n; i++) {
    if (inq[i]) continue;
    int u = i;
    dis[u] = max(flag[u][0], flag[u][1]);
    if (flag[u][0] < flag[u][1])
      col[u] = 1;
    else
      col[u] = 0;
  }
  if (dis[1] < inf)
    printf("%d\n", dis[1]);
  else
    printf("-1\n");
  for (int i = 1; i <= n; i++) printf("%d", col[i]);
  printf("\n");
  return 0;
}
