// G++ MACRO.CPP -STD=C++14
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;
using namespace std;

#define dump(x)  cout << #x << " = " << (x) << endl;
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE"  ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible"  ) << endl
#define possible(n) cout << ((n) ? "possible" : "impossible"  ) << endl

#define SANKOU(n,a,b) cout << ((n) ? (#a) : (#b) ) << endl

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)

#define FOREACH(x,a) for(auto& (x) : (a) )

#define WFA(d,v) REP(k,v)REP(i,v)REP(j,v)d[i][j]=min(d[i][j],d[i][k]+d[k][j])

#define SCOUT(x) cout<<(x)<<" "
#define ENDL cout<<endl

#define VECCIN(x) for(auto&youso_: (x) )cin>>youso_
#define VECIN2(x,y) REP(i,x.size())cin>>x[i]>>y[i]
#define VECCOUT(x) if(1){for(auto tt=x.begin();tt!=x.end();tt++){if(tt!=x.begin())cout<<" ";cout<<(*tt);}cout<<endl;}

#define ALL(obj) (obj).begin(),(obj).end()

#define EXIST(n,x) (find(ALL(n),x)!=n.end())
#define UNIQUE(obj) sort(ALL( obj )); obj.erase(unique(ALL(obj)),obj.end())
#define EN(x) if(1){cout<<#x<<endl;return 0;}
#define COUT(x) cout<<(x)<<endl
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
    cin>>head;
    CINT(move(tail)...);
}
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)

template <class T = ll>
T IN(){T x;cin>>x;return (x);}

template <class Head>
void VT(Head head){}
template <class Head,class Seco,class... Tail>
void VT(Head&& head,Seco&& seco,Tail&&... tail){
    seco.resize(head);
    VT(head,move(tail)...);
}
void VT2(){}
template <class Head,class... Tail>
void VT2(Head&& head,Tail&&... tail){
  VECCIN(head);
  VT2(move(tail)...);
}

template <class Head>
void VT3(Head&& head){}
template <class Head,class Seco,class... Tail>
void VT3(Head&& head,Seco&& seco,Tail&&... tail){
  seco[head]=IN();
  VT3(head,move(tail)...);
}

#define VC1(n,...) V __VA_ARGS__;VT(n,__VA_ARGS__);VT2(__VA_ARGS__); //aaabbbccc
#define VC2(n,...) V __VA_ARGS__;VT(n,__VA_ARGS__);REP(i,n)VT3(i,__VA_ARGS__); //abcabcabc

// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision; // cpp_int

#define P pair<ll,ll>
#define V vector<ll>
#define M map<ll,ll>
#define S set<ll>
#define PQ priority_queue<int>
#define PQG priority_queue<int,V,greater<int>>

#define str string

#define pb(a) push_back(a)
#define mp make_pair

class Dijkstra{
public:
    Dijkstra(vector<vector<P>> ed,ll firs,ll pnum);
    void Solve();
    V range;// range[a]=b;
    vector<vector<P>> edges;// [a][b]=c;
    priority_queue<P,vector<P>,greater<P>> que;// queue=(range,poll);
};

Dijkstra::Dijkstra(vector<vector<P>> ed,ll firs,ll pnum){
    edges=ed;
    V ttt(pnum,LINF);
    range=ttt;
    range[firs]=0;
    que.push(mp(0,firs));
}

void Dijkstra::Solve(){
    while(!que.empty()){
        P top_=que.top();
        que.pop();
        FOREACH(x,edges[top_.second]){
            if(range[x.first] > range[top_.second] + x.second){
                range[x.first] = range[top_.second] + x.second;
                que.push(mp(range[x.first],x.first));
            }
        }
    }
}
int main(){
  ll n,m;cin>>n>>m;V p(m);V q(m);V c(m);
  REP(i,m)cin>>p[i]>>q[i]>>c[i];
  REP(i,m){p[i]--;q[i]--;}
  vector<S> comps(n);
  // map<P,map<P,ll>> edges;

  map<P,ll> map_;//1e5+valになる

  ll co=1;
  REP(i,m){
    if(!map_.count(mp(p[i],c[i]))){
      map_[mp(p[i],c[i])]=1e5+co;
      co++;
    }
    if(!map_.count(mp(q[i],c[i]))){
      map_[mp(q[i],c[i])]=1e5+co;
      co++;
    }
  }
  vector<vector<P>> edges(1e5+co+1);
  REP(i,m){
    edges[ map_[mp(p[i],c[i])] ].pb(mp(map_[mp(q[i],c[i])],0));
    edges[ map_[mp(q[i],c[i])] ].pb(mp(map_[mp(p[i],c[i])],0));
    comps[p[i]].insert(c[i]);
    comps[q[i]].insert(c[i]);
  }
  REP(i,n){
    FOREACH(xx,comps[i]){
      edges[map_[mp(i,xx)]].pb(mp(i,1));
      edges[i].pb(mp(map_[mp(i,xx)],0));
      //edges[mp(i,xx)][mp(i,0)]=1;
      //edges[mp(i,0)][mp(i,xx)]=0;
    }
  }

  /*
  REP(i,m){
    edges[mp(p[i],c[i])][mp(q[i],c[i])]=0;
    edges[mp(q[i],c[i])][mp(p[i],c[i])]=0;
    comps[p[i]].insert(c[i]);
    comps[q[i]].insert(c[i]);
  }
  REP(i,n){
    FOREACH(xx,comps[i]){
      edges[mp(i,xx)][mp(i,0)]=1;
      edges[mp(i,0)][mp(i,xx)]=0;
    }
  }
  */

  Dijkstra dijk(edges,0,1e5+co+1);
  dijk.Solve();
  if(dijk.range[n-1]==LINF)EN(-1);
  COUT(dijk.range[n-1]);



  return 0;
}


Measured memory_bytes: 180736