//  Copyright 2020 Nikita Golikov

#include <bits/stdc++.h>

using namespace std;

using i64 = int64_t;
using ui64 = uint64_t;

#ifdef GOLIKOV
mt19937_64 rng(566);
#else
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#endif

template <typename T>
T rnd_helper(T from, T to, true_type) {
  return uniform_int_distribution<T>(from, to)(rng);
}

template <typename T>
T rnd_helper(T from, T to, false_type) {
  return uniform_real_distribution<T>(from, to)(rng);
}

template <typename T>
T rnd(T from, T to) {
  return rnd_helper(from, to, is_integral<T>{});
}

template <typename A, typename B, typename Cmp>
bool set_min(A &x, B const &y, Cmp &&cmp) {
  if (cmp(y, x)) {
    x = y;
    return true;
  }
  return false;
}

template <typename A, typename B>
bool set_min(A &x, B const &y) {
  return set_min(x, y, less<typename common_type<A, B>::type>{});
}

template <typename A, typename B>
bool set_max(A &x, B const &y) {
  return set_min(x, y, greater<typename common_type<A, B>::type>{});
}

#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
                         tree_order_statistics_node_update>;

template <typename K, typename V>
using ordered_map = tree<K, V, less<K>, rb_tree_tag,
                         tree_order_statistics_node_update>;

//  https://codeforces.com/blog/entry/62393
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t
        FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
    return static_cast<size_t>(splitmix64(x + FIXED_RANDOM));
  }
};

#ifdef __cpp_deduction_guides
template <typename F>
struct rec_fun {
  F fun;

  rec_fun(F&& fun_) : fun(std::forward<F>(fun_)) {}

  template <typename... Args>
  decltype(auto) operator()(Args&&... args) const {
    return fun(*this, std::forward<Args>(args)...);
  }
};
#endif

#ifdef __cpp_inline_variables
template <typename T>
inline constexpr auto type_min = numeric_limits<T>::min();

template <typename T>
inline constexpr auto type_max = numeric_limits<T>::max();
#endif

//  fast alloc
#if 0
int const MAX_MEM = 1e8;
int mpos = 0;
char mem[MAX_MEM];
void* operator new(size_t n) {
  mpos += n;
  return (void*)(mem + mpos - n);
}
void operator delete (void*) noexcept {}
#endif

//  pragmas
#if 0
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("unroll-loops")
#endif

int const N = (int) 1e5 + 20;

vector<pair<int, int>> graph[N];

int main() {
#ifdef GOLIKOV
  assert(freopen("in", "rt", stdin));
  auto _clock_start = chrono::high_resolution_clock::now();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> possible;
  for (int i = 0; i < m; ++i) {
    int u, v, c;
    cin >> u >> v >> c;
    --u, --v;
    graph[u].emplace_back(c, v);
    graph[v].emplace_back(c, u);
    possible.emplace_back(u, c);
    possible.emplace_back(v, c);
  }
  possible.emplace_back(0, -1);
  sort(possible.begin(), possible.end());
  possible.erase(unique(possible.begin(), possible.end()), possible.end());
  auto get_pos = [&](int v, int c) {
    return lower_bound(possible.begin(), possible.end(), make_pair(v, c)) -
      possible.begin();
  };
  int npos = (int) possible.size();
  for (int i = 0; i < n; ++i) {
    sort(graph[i].begin(), graph[i].end());
  }
  int const INF = 2 * n + 10;
  vector<int> d(n, INF);
  d[0] = 0;
  vector<int> d_color(npos, INF);
  deque<pair<int, int>> q;
  for (int i = 0; i < npos; ++i) {
    if (possible[i].first) break;
    d_color[i] = 0;
  }
  q.emplace_back(0, -1);
  vector<bool> used(n);
  vector<bool> used_color(npos);
  while (!q.empty()) {
    auto v = q.front().first;
    auto c = q.front().second;
    q.pop_front();
    auto pos = get_pos(v, c);
    if (used_color[pos]) {
      continue;
    }
    used_color[pos] = true;
    auto go = [&](auto start, auto end) {
      for (auto it = start; it != end; ++it) {
        auto cu = it->first;
        auto u = it->second;
        auto diff = c != cu;
        auto new_dist = d_color[pos] + diff;
        if (set_min(d_color[get_pos(u, cu)], new_dist)) {
          set_min(d[u], new_dist);
          if (diff) {
            q.emplace_back(u, cu);
          } else {
            q.emplace_front(u, cu);
          }
        }
      }
    };
    if (!used[v]) {
      go(graph[v].begin(), graph[v].end());
      used[v] = true;
    }
    go(lower_bound(graph[v].begin(), graph[v].end(), make_pair(c, 0)),
        lower_bound(graph[v].begin(), graph[v].end(), make_pair(c + 1, 0)));
  }
  cout << (d.back() == INF ? -1 : d.back()) << '\n';

#ifdef GOLIKOV
  cerr << "Executed in " << chrono::duration_cast<chrono::milliseconds>(
      chrono::high_resolution_clock::now()
          - _clock_start).count() << "ms." << endl;
#endif
  return 0;
}


Measured memory_bytes: 18340