#include <bits/stdc++.h>
using namespace std;
const int N = (int)5e5 + 50, INF = (int)1e9;
struct edge {
  int to, c;
};
int n, m;
vector<edge> G[N];
int d[N][2], res[N];
set<pair<int, int> > S;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < (m); ++i) {
    int u, v, c;
    cin >> u >> v >> c;
    u--, v--;
    G[v].push_back({u, c});
  }
  for (int i = 0; i < n; i++) d[i][0] = d[i][1] = INF;
  d[n - 1][0] = d[n - 1][1] = 0;
  for (int i = 0; i < (n); ++i) S.insert({max(d[i][0], d[i][1]), i});
  while (!S.empty()) {
    auto p = *S.begin();
    S.erase(S.begin());
    int val, v;
    tie(val, v) = p;
    res[v] = val;
    for (auto& e : G[v]) {
      if (S.find({max(d[e.to][0], d[e.to][1]), e.to}) == S.end()) continue;
      S.erase({max(d[e.to][0], d[e.to][1]), e.to});
      d[e.to][e.c] = min(d[e.to][e.c], val + 1);
      S.insert({max(d[e.to][0], d[e.to][1]), e.to});
    }
  }
  cout << (res[0] == INF ? -1 : res[0]) << '\n';
  for (int i = 0; i < (n); ++i) {
    cout << (d[i][1] > d[i][0]);
  }
  cout << '\n';
}
