#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}

#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()

/* do your best */

// verified : http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A
// 負辺があると動作しない?
// O(E log V)
template<typename T>
vector<T> dijkstra(vector<vector<pair<int, T>>> &g, int s){
     
  int n = g.size();
  // numeric_limits がうまく使えない場合は自分で INF を定義する.
  const auto inf = numeric_limits<T>::max();
  vector<T> dist(n, inf);

  using P = pair<T, int>;
  // 小さい順に取り出せる
  priority_queue<P, vector<P>, greater<P>> que;
  dist[s] = 0;
  que.emplace(dist[s], s);
  while(!que.empty()){
    T cost;
    int node;
    tie(cost, node) = que.top();
    que.pop();
    if(dist[node] < cost) continue;
    for(auto &e : g[node]){
      auto next_cost = cost + e.second;
      int next_node = e.first;
      if(dist[next_node] <= next_cost) continue;
      dist[next_node] = next_cost;
      que.emplace(next_cost, next_node);
    }
  }

  return dist;
}
int main() {
  
  int n, m; cin >> n >> m;
  // 各頂点にはどの会社があるか
  vector<set<int>> com(n);
  vector<int> p(m);
  vector<int> q(m);
  vector<int> c(m);
  for (int i = 0; i < m; i++) {
    cin >> p[i] >> q[i] >> c[i];
    p[i]--;
    q[i]--;
    com[p[i]].insert(c[i]);
    com[q[i]].insert(c[i]);
  }

  // 昇順に，各駅にある会社を登録
  // (頂点, 会社) :: 一番最後が改札の外
  int node = 0;
  vector<vector<int>> toNode(n);
  vector<vector<int>> info(n);


  for (int i = 0; i < n; i++) {
    for (auto e : com[i]) {
      info[i].push_back(e);
      toNode[i].push_back(node);
      node++;
    }
    // 改札に push
    toNode[i].push_back(node);
    node++;
  }


  vector<vector<pair<int, int>>> g(node);

  for (int i = 0; i < n; i++) {
    //  -> 改札の外
    for (int j = 0; j < info[i].size(); j++) {
      int curNode = toNode[i][j];
      int nxtNode = toNode[i][info[i].size()];
      int cost = 0;
      g[curNode].push_back({nxtNode, cost});
    }

    // -> 改札の中
    for (int j = 0; j < info[i].size(); j++) {
      int curNode = toNode[i][info[i].size()];
      int nxtNode = toNode[i][j];
      int cost = 1;
      g[curNode].push_back({nxtNode, cost});
    }
  }


  // 会社 c は，この駅では何番目
  vector<map<int, int>> inv(n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < info[i].size(); j++) {
      inv[i][info[i][j]] = j;
    }
  }


  // 改札の中で，同じ会社ならタダ乗りできる
  for (int i = 0; i < m; i++) {
    {
      int curNode = toNode[p[i]][inv[p[i]][c[i]]];
      int nxtNode = toNode[q[i]][inv[q[i]][c[i]]];
      int cost = 0;
      g[curNode].push_back({nxtNode, cost});
      g[nxtNode].push_back({curNode, cost});


    }
  }

  int S = toNode[0][info[0].size()];
  int G = toNode[n - 1][info[n - 1].size()];

  int ans = dijkstra(g, S)[G];
  if (ans == numeric_limits<int>::max()) ans = -1;
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 95132