#include <bits/stdc++.h>
using namespace std;
const int SIZE = 1100000;
void imax(int &a, int b) { a = max(a, b); }
void imin(int &a, int b) { a = min(a, b); }
void lmax(long long &a, long long b) { a = max(a, b); }
void lmin(long long &a, long long b) { a = min(a, b); }
int DP[SIZE], A[SIZE], B[SIZE];
vector<int> graph[SIZE];
bool visited[SIZE];
queue<int> Q;
int n, m, x, y, z;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  while (m--) cin >> x >> y >> z, graph[y].push_back(x * ((-2) * z + 1));
  for (int i = 0; i < n; i++) DP[i] = A[i] = B[i] = SIZE;
  Q.push(n);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    if (visited[u]) continue;
    visited[u] = true;
    for (auto v : graph[u])
      if (!visited[abs(v)])
        imin(((v > 0) ? A[abs(v)] : B[abs(v)]), DP[u] + 1),
            DP[abs(v)] = max(A[abs(v)], B[abs(v)]),
            (DP[abs(v)] != SIZE) ? Q.push(abs(v)) : Q.push(n);
  }
  cout << ((DP[1] == SIZE) ? -1 : DP[1]) << "\n";
  for (int i = 1; i <= n; i++) cout << (A[i] < B[i]);
  cout << "\n";
  return 0;
}
