// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <algorithm>

#include <climits>

#include <cstring>

#include <string>

#include <set>

#include <map>

#include <queue>

#include <stack>

#include <vector>

#include <list>

#define rep(i,m,n) for(i=m;i<=n;i++)

#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)

#define mod 1000000007

#define inf 0x3f3f3f3f

#define vi vector<int>

#define pb push_back

#define mp make_pair

#define fi first

#define se second

#define ll long long

#define pi acos(-1.0)

#define pii pair<int,int>

#define Lson L, mid, rt<<1

#define Rson mid+1, R, rt<<1|1

const int maxn=1e5+10;

using namespace std;

ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}

ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

int n,m,k,t,h[maxn],tot,ans[maxn];

set<int>comp[maxn];

struct node

{

    int to,nxt,com;

}e[maxn<<2];

void add(int x,int y,int z)

{

    tot++;

    e[tot].to=y;

    e[tot].com=z;

    e[tot].nxt=h[x];

    h[x]=tot;

}

priority_queue<pair<int,int> >p;

int main()

{

    int i,j;

    memset(ans,inf,sizeof ans);

    scanf("%d%d",&n,&m);

    while(m--)

    {

        int a,b,c;

        scanf("%d%d%d",&a,&b,&c);

        add(a,b,c);

        add(b,a,c);

    }

    p.push({0,1});ans[1]=0;

    while(!p.empty())

    {

        int now=p.top().se,ca=-p.top().fi;

        p.pop();

        if(ans[now]<ca)continue;

        for(i=h[now];i;i=e[i].nxt)

        {

            int to=e[i].to,to_com=e[i].com;

            int to_ca=ca+(!comp[now].count(to_com));

            if(ans[to]>to_ca)

            {

                ans[to]=to_ca;

                p.push({-to_ca,to});

                comp[to].clear();

                comp[to].insert(to_com);

            }

            else if(ans[to]==to_ca)

            {

                comp[to].insert(to_com);

            }

        }

    }

    printf("%d\n",ans[n]==inf?-1:ans[n]);

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.6063129145652464x):
#include <cstdio>

#include <algorithm>

#include <map>

#include <iostream> 

#include <queue>

#include <cstring>

#define INF 0x3f3f3f3f



const int MAXN = 1e5 + 1, MAXM = 2e5 + 1;



int n, m;



struct Edge { int ver, nxt, val; } edge[MAXM << 3];

int head[MAXN << 3], tot = 0;

inline void Ins(int x, int y, int z) { edge[++tot].ver = y, edge[tot].val = z, edge[tot].nxt = head[x], head[x] = tot; }



std :: map<std :: pair<int, int>, int> ex;

int cnt;

#define mp std :: make_pair

inline int id(int x, int num) { return ex[mp(x, num)] ? ex[mp(x, num)] : ex[mp(x, num)] = ++cnt; }



std :: deque<int> q; int d[MAXN << 3];

void BFS() {

    memset(d, 0x3f, sizeof d);

    d[1] = 0; q.push_front(1);

    while (q.size()) {

        int x = q.front(); q.pop_front();

        for (int i = head[x]; i; i = edge[i].nxt) {

            int y = edge[i].ver, z = edge[i].val;

            if (d[y] > d[x] + z) {

                d[y] = d[x] + z;

                !z ? q.push_front(y) : q.push_back(y);

            }

        }

    }

}



int main() {

    scanf("%d%d", &n, &m); cnt = n;

    for (int i = 1; i <= m; ++i) {

        int u, v, w; scanf("%d%d%d", &u, &v, &w);

        int p = id(u, w), q = id(v, w);

        Ins(u, p, 1), Ins(q, v, 1), Ins(p, q, 0);

        Ins(p, u, 1), Ins(v, q, 1), Ins(q, p, 0);

    }

    BFS();

    printf("%d\n", d[n] == INF ? -1 : d[n] >> 1);

    return 0;

}