#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast", "01")
const int maxn = 5e5 + 5;
int n, m;
vector<int> v[maxn][2];
vector<int> g[maxn][2];
vector<int> vv[maxn];
vector<int> gg[maxn];
set<pair<int, pair<int, int> > > s;
int w[maxn];
int mark[maxn];
queue<int> q;
int d[maxn];
int asd[maxn];
void BFS() {
  while (q.size()) {
    int x = q.front();
    q.pop();
    if (asd[x]) {
      continue;
    }
    asd[x] = 1;
    for (int i = 0; i < gg[x].size(); i++) {
      int y = gg[x][i];
      d[y] = min(d[y], d[x] + 1);
      if (!mark[y]) {
        mark[y] = 1;
        q.push(y);
      }
    }
    for (int i = 0; i < g[x][1].size(); i++) {
      int y = g[x][1][i];
      if (w[y] == -1) {
        w[y] = 0;
      }
      if (w[y] == 1 && !mark[y]) {
        q.push(y);
        d[y] = min(d[y], d[x] + 1);
        mark[y] = 1;
      }
    }
    for (int i = 0; i < g[x][0].size(); i++) {
      int y = g[x][0][i];
      if (w[y] == -1) {
        w[y] = 1;
      }
      if (w[y] == 0 && !mark[y]) {
        d[y] = min(d[y], d[x] + 1);
        q.push(y);
        mark[y] = 1;
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y, t;
    cin >> x >> y >> t;
    v[x][t].push_back(y);
    g[y][t].push_back(x);
    int cnt = s.size();
    int g = 1;
    if (t) {
      g = 0;
    }
    s.insert(make_pair(g, make_pair(x, y)));
    if (s.size() == cnt) {
      vv[x].push_back(y);
      gg[y].push_back(x);
    } else {
      s.erase(make_pair(g, make_pair(x, y)));
      s.insert(make_pair(t, make_pair(x, y)));
    }
  }
  for (int i = 1; i <= n; i++) {
    w[i] = -1;
    d[i] = 1e18;
  }
  d[n] = 0;
  mark[n] = 1;
  q.push(n);
  BFS();
  if (mark[1]) {
    cout << d[1] << '\n';
  } else {
    cout << -1 << '\n';
  }
  for (int i = 1; i <= n; i++) {
    if (w[i] != -1) {
      cout << w[i];
    } else {
      cout << 1;
    }
  }
}
