#include <bits/stdc++.h>
using namespace std;
const int MAXN = 500100;
const int INF = 0x3f3f3f3f;
int n, m;
vector<pair<int, int>> grafo[MAXN];
bool vis[MAXN];
int cor[MAXN];
int dist[MAXN];
void djkstra() {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      fila;
  fila.push({0, n});
  dist[n] = 0;
  while (!fila.empty()) {
    pair<int, int> at = fila.top();
    if (vis[at.second] == true) {
      fila.pop();
      continue;
    }
    vis[at.second] = true;
    for (auto i : grafo[at.second]) {
      if (vis[i.first] == true) continue;
      if (cor[i.first] == -1) {
        cor[i.first] = (i.second + 1) % 2;
        continue;
      }
      if (cor[i.first] != i.second) continue;
      if (at.first + 1 < dist[i.first]) {
        dist[i.first] = at.first + 1;
        fila.push({dist[i.first], i.first});
      }
    }
  }
}
int main() {
  memset(cor, -1, sizeof cor);
  memset(dist, INF, sizeof dist);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    grafo[y].push_back({x, z});
  }
  djkstra();
  if (dist[1] == INF)
    cout << -1 << endl;
  else
    cout << dist[1] << endl;
  for (int i = 1; i <= n; i++) {
    if (cor[i] == -1)
      cout << 1;
    else
      cout << cor[i];
  }
}
