#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ", ";
  err(++it, args...);
  cout << "\n";
}
long long powm(long long a, long long b, long long mod) {
  long long res = 1;
  while (b) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
const long long mod = 1e9 + 7;
const long long N = 1000005;
const long long inf = 1e9;
void solve() {
  int n, m;
  cin >> n >> m;
  if (n == 1) {
    cout << "0 1\n";
    return;
  }
  vector<pair<int, int>> adj[n];
  for (long long i = 0; i < (m); i++) {
    int x, y, t;
    cin >> x >> y >> t;
    x--, y--;
    adj[y].push_back({x, t});
  }
  set<pair<int, int>> s;
  vector<array<int, 2>> dp(n, {inf, inf});
  dp[n - 1][0] = dp[n - 1][1] = 0;
  s.insert({0, n - 1});
  while (!s.empty()) {
    int d = s.begin()->first;
    int i = s.begin()->second;
    s.erase(s.begin());
    for (pair<int, int> p : adj[i]) {
      int j = p.first;
      int type = p.second;
      if (d + 1 < dp[j][type]) {
        auto it = s.find(make_pair(max(dp[j][0], dp[j][1]), j));
        if (it != s.end()) s.erase(it);
        dp[j][type] = d + 1;
        s.insert({max(dp[j][0], dp[j][1]), j});
      }
    }
  }
  cout << (max(dp[0][0], dp[0][1]) == inf ? -1 : max(dp[0][0], dp[0][1]))
       << '\n';
  for (long long i = 0; i < (n); i++) cout << (dp[i][0] > dp[i][1] ? 0 : 1);
  cout << '\n';
  ;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);
  int tc = 1;
  while (tc--) {
    solve();
  }
  return 0;
}
