#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 3, inf = 1e9;
vector<pair<int, int> > v[N];
int n, m, d[N], ans[N];
void solve() {
  for (int i = 1; i <= n; i++) d[i] = inf;
  d[n] = 0;
  memset(ans, -1, sizeof ans);
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  q.push({0, n});
  while (!q.empty()) {
    int node = q.top().second, cost = q.top().first;
    q.pop();
    for (auto i : v[node]) {
      if (cost + 1 >= d[i.first]) continue;
      if (ans[i.first] == -1) ans[i.first] = 1 - i.second;
      if (ans[i.first] != i.second) continue;
      d[i.first] = cost + 1;
      q.push({d[i.first], i.first});
    }
  }
}
int main() {
  ios_base::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[y].push_back({x, t});
  }
  solve();
  if (d[1] == inf) d[1] = -1;
  cout << d[1] << "\n";
  for (int i = 1; i <= n; i++) {
    if (ans[i] == -1) ans[i] = 1;
    cout << ans[i];
  }
}
