#include <bits/stdc++.h>
using namespace std;
void tempwj() {
  freopen("hash.in", "r", stdin);
  freopen("hash.out", "w", stdout);
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long qpow(long long a, long long b, long long mod) {
  a %= mod;
  long long ans = 1;
  while (b > 0) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
struct cmp {
  bool operator()(const pair<int, int>& a, const pair<int, int>& b) {
    return a.second > b.second;
  }
};
int lb(int x) { return x & -x; }
const int inf = 0x3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long mod = 1e9 + 7;
const int maxn = 5e5 + 10;
const int M = 1e6 + 2;
int dis[maxn];
int vis[maxn];
std::vector<pair<int, int> > vv[maxn];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    dis[i] = inf;
    vis[i] = -1;
  }
  for (int i = 1; i <= m; i++) {
    int x, y, v;
    scanf("%d%d%d", &x, &y, &v);
    vv[y].push_back(make_pair(x, v));
  }
  queue<int> qq;
  qq.push(n);
  dis[n] = 0;
  while (!qq.empty()) {
    int x = qq.front();
    qq.pop();
    for (int i = 0; i < vv[x].size(); i++) {
      int v = vv[x][i].first;
      if (vis[v] == -1) {
        vis[v] = vv[x][i].second ^ 1;
      } else {
        if (vv[x][i].second == vis[v] && dis[v] > dis[x] + 1) {
          dis[v] = dis[x] + 1;
          qq.push(v);
        }
      }
    }
  }
  if (dis[1] == inf) {
    printf("-1\n");
  } else
    printf("%d\n", dis[1]);
  for (int i = 1; i <= n; i++) {
    if (vis[i] == -1) vis[i] = 0;
    printf("%d", vis[i]);
  }
  printf("\n");
}
