#define STOPIT
#define NDEBUG
#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_a_genius = 0; ngtkana_is_a_genius < int(n); ngtkana_is_a_genius++)
#define rep(i, begin, end) for (int i = int(begin); (i) < int(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto mt = std::mt19937_64(std::random_device{}());
auto cmn = [](auto& a, auto b){ if (a > b) {a = b; return true;} return false; };
auto cmx = [](auto& a, auto b){ if (a < b) {a = b; return true;} return false; };
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) { std::cerr << " " << head; debug_impl(tail...); }
#ifndef STOPIT
#define debug(...)\
  do {\
    std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
    debug_impl(__VA_ARGS__);\
    std::cerr << std::noboolalpha;\
  } while (false)
#else
#define debug(...) 0;
#endif

#define at operator[]

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr>
std::istream& operator>> (std::istream& is, Container& v)
  { for (auto & x : v) { is >> x; } return is; }

template < typename Container, typename Value = typename Container::value_type, std::enable_if_t<!std::is_same< Container, std::string >::value, std::nullptr_t> = nullptr >
std::ostream& operator<< (std::ostream& os, Container const& v) {
 os << "{";
  for (auto it = v.begin(); it != v.end(); it++)
    {os << (it != v.begin() ? "," : "") << *it;}
  return os << "}";
}

template < template < typename ... > class Tuple,  typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::istream& tuple_input_impl(std::istream& os, Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>)
  { (void)std::initializer_list<int>{((void)(os >> std::get< Inds >(tuple)), 0)...}; return os; }
template < template < typename ... > class Tuple,  typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::istream& operator>> (std::istream& os, Tuple<Args...>& tuple)
  { return tuple_input_impl(os, tuple, std::index_sequence_for<Args...>()); }

template < template < typename ... > class Tuple,  typename... Args, std::size_t ... Inds, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::ostream& tuple_output_impl(std::ostream& os, const Tuple<Args...>& tuple, std::integer_sequence<std::size_t, Inds...>)
  { os << "("; (void)std::initializer_list<int>{((void)(os << (Inds > 0 ? "," : "") << std::get< Inds >(tuple)), 0)...}; return os << ")"; }
template < template < typename ... > class Tuple,  typename... Args, std::size_t = std::tuple_size< Tuple < Args ... > >::value >
std::ostream& operator<< (std::ostream& os, const Tuple<Args...>& tuple)
 { return tuple_output_impl(os, tuple, std::index_sequence_for<Args...>()); }

template < class Value >
struct vending_machine{
  Value i;
  vending_machine(Value i) : i(i){}
  auto issue() { return i++; }
  auto peek() const { return i; }
};
template < class Value >
auto make_vending_machine(Value i)
  { return vending_machine< Value >(i); }

int main() {
  std::cin.tie(0); std::cin.sync_with_stdio(false);
  int n, m; std::cin >> n >> m;
  std::vector< std::tuple< int, int, int > > data(m);
  int kind = 0;
  // std::vector< int > master(m);
  rep(i,0,m) {
    int u, v, c;
    std::cin >> u >> v >> c;
    u--, v--;
    data.at(i) = std::make_tuple(u, v, c);
    cmx(kind, c);
    // master.at(i) = c;
  }
  kind++;
  // std::sort(all(master));
  // master.resize(std::unique(all(master)) - master.begin());
  // int kind = master.size();
  // for (auto&& tuple : data) {
  //   int & c = std::get< 2 >(tuple);
  //   int new_c = std::lower_bound(all(master), c) - master.begin();
  //   assert(master.at(new_c) == c);
  //   c = new_c;
  // }
  debug(data);

  std::vector< std::map< int, std::vector< int > > > bgraphs(n);
  // std::vector< std::vector< std::pair< int, int > > > bgraph(n);
  std::vector< std::vector< int > > member(kind);
  for (auto&& tuple : data) {
    int u, v, c; std::tie(u, v, c) = tuple;
    member.at(c).emplace_back(u);
    member.at(c).emplace_back(v);
    // bgraph.at(u).emplace_back(v, c);
    // bgraph.at(v).emplace_back(u, c);
    bgraphs.at(u)[c].emplace_back(v);
    bgraphs.at(v)[c].emplace_back(u);
  }
  debug(member);
  // debug(bgraph);
  debug(bgraphs);

  std::vector< std::vector< int > > graph(n);
  auto insert = [&] (int u, int v)
  {
    graph.at(u).emplace_back(v);
    graph.at(v).emplace_back(u);
  };
  vending_machine< int > vm(n);
  std::vector< int > ckd_(n, -1);
  rep(i,0,kind) {
    debug(i);
    auto const& mem = member.at(i);
    for (int start : mem) {
      if (ckd_.at(start) == i) continue;
      graph.emplace_back();

      auto dfs = [&](auto&& dfs, int crr) -> void {
        assert(ckd_.at(crr) != i);
        ckd_.at(crr) = i;
        insert(crr, vm.peek());
        for (int nxt : bgraphs.at(crr)[i]) {
          if (ckd_.at(nxt) == i) continue;
          dfs(dfs, nxt);
        }
      };
      dfs(dfs, start);
      vm.issue();
    }
    debug(ckd_);
  }
  debug(graph);

  int N = vm.peek();
  int s = 0, t = n - 1;
  std::vector< int > ckd(N, false);
  std::vector< int > dist(N, -1);
  std::queue< int > que;
  que.emplace(s);
  dist.at(s) = 0;
  while (!que.empty()) {
    int crr = que.front(); que.pop();
    if (ckd.at(crr)) continue;
    debug(crr);
    ckd.at(crr) = true;
    for (int nxt : graph.at(crr)) {
      if (ckd.at(nxt)) continue;
      dist.at(nxt) = dist.at(crr) + 1;
      que.emplace(nxt);
    }
  }
  debug(dist);
  int ans = dist.at(t);
  if (ans != -1) {
    assert(ans % 2 == 0);
    ans /= 2;
  }
  std::cout << ans << std::endl;
  return 0;
}

Measured memory_bytes: 101896