#include <bits/stdc++.h>
using namespace std;
long long int mpw(long long int a, long long int b, long long int m) {
  long long int ans = 1;
  if (a == 0) return 0;
  if (a == 1 || b == 0) return 1;
  while (b) {
    if (b & 1) {
      ans = (ans * a) % m;
    }
    a = (a * a) % m;
    b >>= 1;
  }
  return ans;
}
long long int pw(long long int a, long long int b) {
  long long int ans = 1;
  if (a == 0) return 0;
  if (a == 1 || b == 0) return 1;
  while (b) {
    if (b & 1) {
      ans *= a;
    }
    a *= a;
    b >>= 1;
  }
  return ans;
}
long long int mn(long long int a, long long int b) {
  if (a <= b) {
    return a;
  }
  return b;
}
long long int mx(long long int a, long long int b) {
  if (a >= b) {
    return a;
  }
  return b;
}
struct line {
  long long int m, c;
  long long int eval(long long int x) { return m * 1LL * x + c; }
  long long int intersect(line l) { return (long double)(c - l.c) / (l.m - m); }
};
struct comp {
  bool operator()(pair<int, int> l, pair<int, int> r) {
    return l.first > r.first;
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  const int inf = 1e9 + 7;
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> g(n);
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    g[b].push_back({a, c});
  }
  vector<int> clr(n, 0);
  vector<vector<int>> dist(n, vector<int>(2, inf));
  dist[n - 1][0] = 0;
  dist[n - 1][1] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>, comp> pq;
  pq.push({0, n - 1});
  vector<int> vis(n, 0);
  while (pq.size() > 0) {
    pair<int, int> p = pq.top();
    pq.pop();
    if (vis[p.second] == 1) {
      continue;
    }
    vis[p.second] = 1;
    for (int i = 0; i < g[p.second].size(); ++i) {
      dist[g[p.second][i].first][g[p.second][i].second] =
          min(dist[g[p.second][i].first][g[p.second][i].second], p.first + 1);
      pq.push(
          {max(dist[g[p.second][i].first][0], dist[g[p.second][i].first][1]),
           g[p.second][i].first});
    }
  }
  int ans = max(dist[0][0], dist[0][1]);
  if (ans >= inf) {
    cout << "-1\n";
  } else {
    cout << ans << "\n";
  }
  string s = "";
  for (int i = 0; i < n; ++i) {
    s += (dist[i][0] > dist[i][1] ? '0' : '1');
  }
  cout << s << "\n";
  return 0;
}
