#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("-ffloat-store")
using namespace std;
const auto start_time = std::chrono::high_resolution_clock::now();
void aryanc403() {}
const long long int INF = 0xFFFFFFFFFFFFFFFL;
long long int seed;
mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count());
inline long long int rnd(long long int l = 0, long long int r = INF) {
  return uniform_int_distribution<long long int>(l, r)(rng);
}
class CMP {
 public:
  bool operator()(pair<long long int, long long int> a,
                  pair<long long int, long long int> b) {
    return !(a.first < b.first || (a.first == b.first && a.second <= b.second));
  }
};
void add(map<long long int, long long int> &m, long long int x,
         long long int cnt = 1) {
  auto jt = m.find(x);
  if (jt == m.end())
    m.insert({x, cnt});
  else
    jt->second += cnt;
}
void del(map<long long int, long long int> &m, long long int x,
         long long int cnt = 1) {
  auto jt = m.find(x);
  if (jt->second <= cnt)
    m.erase(jt);
  else
    jt->second -= cnt;
}
bool cmp(const pair<long long int, long long int> &a,
         const pair<long long int, long long int> &b) {
  return a.first < b.first || (a.first == b.first && a.second < b.second);
}
const long long int mod = 1000000007L;
long long int T, n, i, j, k, in, cnt, l, r, u, v, x, y;
long long int m;
string s;
vector<long long int> dp, col, vis, par;
vector<vector<long long int> > e, er;
void dijk() {
  queue<long long int> q;
  q.push(2 * n - 2);
  q.push(2 * n - 1);
  q.push(-1);
  int cnt = 0;
  bool fl = false;
  while (!q.empty()) {
    const auto u = q.front();
    q.pop();
    if (u == -1) {
      cnt++;
      if (fl) break;
      fl = true;
      q.push(-1);
      continue;
    }
    if (dp[u / 2] <= cnt) continue;
    if (col[u / 2] == -1) {
      col[u / 2] = !(u & 1);
      continue;
    }
    if (col[u / 2] != u % 2) continue;
    ;
    dp[u / 2] = cnt;
    col[u / 2] = u % 2;
    fl = false;
    for (auto x : er[u]) q.push(x);
  };
}
long long int dijk2() {
  queue<long long int> q;
  vector<long long int> dp(n, INF);
  int cnt = 0;
  bool fl = false;
  q.push(0);
  q.push(-1);
  while (!q.empty()) {
    auto u = q.front();
    q.pop();
    ;
    if (u == -1) {
      cnt++;
      if (fl) break;
      fl = true;
      q.push(-1);
      continue;
    }
    if (dp[u] <= cnt) continue;
    fl = false;
    dp[u] = cnt;
    u = 2 * u + col[u];
    for (auto x : e[u]) q.push(x);
  };
  if (dp[n - 1] == INF) return -1;
  return dp[n - 1];
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  {
    cin >> n >> m;
    if (n == 1) {
      cout << 0 << "\n";
      cout << 0 << "\n";
      return 0;
    }
    dp.resize(n, INF);
    vis.resize(2 * n, -1);
    par.resize(2 * n, -1);
    e.resize(2 * n);
    er.resize(2 * n);
    while (m--) {
      long long int t;
      cin >> u >> v >> t;
      if (u == v) continue;
      u--;
      v--;
      u = 2 * u + t;
      v *= 2;
      e[u].push_back(v / 2);
      er[v].push_back(u);
      er[v + 1].push_back(u);
    }
    col.resize(n, -1);
    dijk();
    for (auto &x : col) x = max(x, 0LL);
    cout << dijk2() << "\n";
    for (auto x : col) cout << x;
    cout << "\n";
  }
  aryanc403();
  return 0;
}
