#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
const int mo = 998244353;
const int inf = 1e9;
int d[2][N];
vector<pair<int, int> > p[N];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (auto i = (1); i <= (m); ++i) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    p[y].push_back({x, z});
  }
  queue<int> q;
  q.push(n);
  for (auto i = (1); i <= (n - 1); ++i) d[0][i] = d[1][i] = inf;
  for (; !q.empty(); q.pop()) {
    int x = q.front(), y = max(d[0][x], d[1][x]) + 1;
    for (auto& i : p[x])
      if (d[i.second][i.first] == inf) {
        d[i.second][i.first] = y;
        if (d[i.second][i.first] != inf && d[i.second ^ 1][i.first] != inf)
          q.push(i.first);
      }
  }
  printf("%d\n", max(d[0][1], d[1][1]) == inf ? -1 : max(d[0][1], d[1][1]));
  for (auto i = (1); i <= (n); ++i) printf("%d", int(d[0][i] < d[1][i]));
}
