#include <bits/stdc++.h>
using namespace std;
struct P {
  int x, y;
  bool operator<(const P &a) const {
    if (y != a.y) return y > a.y;
    return x < a.x;
  };
};
int a, b, c, d, i, n, m, e, k, dx[10] = {1, 0, -1, 0, 1, 1, -1, -1},
                               dy[10] = {0, 1, 0, -1, 1, -1, 1, -1};
int o[510441];
int l[511111];
long long ss[1];
long long x, y, z, mod = 1000000007, hf = (mod + 1) / 2;
char r[1];
vector<P> v[501011];
P u[1];
priority_queue<P> q;
set<int> s;
map<int, int> p;
bool as(P a, P b) { return a.x < b.x; }
int main() {
  scanf("%d %d", &a, &b);
  memset(o, -1, sizeof(o));
  for (int t = 1; t <= b; t++) {
    scanf("%d %d %d", &n, &m, &k);
    v[m].push_back({n, k});
  }
  for (int t = 1; t <= a; t++) l[t] = mod;
  l[a] = 0;
  q.push({a, 0});
  for (; q.size();)
    if (q.top().y == l[q.top().x]) {
      P s = q.top();
      q.pop();
      for (int h = 0; h < v[s.x].size(); h++)
        if (o[v[s.x][h].x] == v[s.x][h].y) {
          if (l[v[s.x][h].x] > s.y + 1) {
            l[v[s.x][h].x] = s.y + 1;
            q.push({v[s.x][h].x, s.y + 1});
          }
        } else if (o[v[s.x][h].x] == -1) {
          o[v[s.x][h].x] = 1 - v[s.x][h].y;
        }
    } else
      q.pop();
  if (l[1] == mod)
    puts("-1");
  else
    printf("%d\n", l[1]);
  for (int t = 1; t <= a; t++)
    if (o[t] < 0)
      printf("1");
    else
      printf("%d", o[t]);
}
