#include<bits/stdc++.h>
using namespace std;
#define f1(a,b,c) for(int c=a;c<=b;c++)
#define f2(a,b,c) for(int c=a;c>=b;c--)
#define f3(a,b,c) for(int c=a;c;c=b)
#define so1(a,n) sort(a+1,a+n+1,mycmp);
#define so2(a,n) sort(a+1,a+n+1);
#define ll long long
#define itn int
#define ubt int 
#define pii pair<int,int>
#define mp make_pair
const int twx=1e6+100;
const int inf=0x7fffffff;
ll read()
{
    ll sum=0;
    ll flag=1;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-')
        {
            flag=-1;
        }
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        sum=((sum*10)+c-'0');
        c=getchar();
    }
    return sum*flag;
}
int n,m;
struct LV
{
    int Next;
    int y;
    int v;
}a[twx<<1];
int Link[twx];
int len;
map<pii,int> p;
int cnt=0;
bool v[twx];
int d[twx];
void Insert(int x,int y,int z)
{
    a[++len].y=y;
    a[len].Next=Link[x];
    a[len].v=z;
    Link[x]=len;
}
int get(int x,int y)
{
    if(p.find(mp(x,y))!=p.end())
    {
        return p[mp(x,y)];     
    }
    else
    {
        return p[mp(x,y)]=++cnt;
    }
}
void spfa()
{
    memset(d,0x3f,sizeof d);
    queue<int> q;
    q.push(1);
    v[1]=1;
    d[1]=0;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        f3(Link[x],a[i].Next,i)
        {
            int y=a[i].y;
            int vs=a[i].v;
            if(d[y]>d[x]+vs)
            {
                d[y]=d[x]+vs;
                if(!v[y])
                {
                    v[y]=1;
                    q.push(y);
                }
            }
        }
        v[x]=0;
    }
}
void init()
{
	n=read();
    m=read();
    cnt=n;
    f1(1,m,i)
    {
        int x=read();
        int y=read();
        int z=read();
        int xz=get(x,z);
        int yz=get(y,z);
        Insert(xz,yz,0);
        Insert(yz,xz,0);
        Insert(x,xz,1);
        Insert(xz,x,1);
        Insert(y,yz,1);
        Insert(yz,y,1);
    }
    spfa();
    printf("%d\n",d[n]==0x3f3f3f3f?-1:d[n]/2);
}
void work()
{
	
}
void print()
{
	
}
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
    init();
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, pii> piipii;

#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define eb emplace_back

int dp[300005];
vector<pii> g[300005];
vector<pii> edge[1000005];
int p[100005];
int idx[100005];
int root(int a){
    return p[a] == a?a:(p[a]=root(p[a]));
}
void merge(int a, int b){
    a = root(a), b = root(b);
    if(a != b) p[a] = b;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);   
    int n, m;
    cin >> n >> m;
    for(int i=1;i<=n;i++) p[i] = i;
    for(int i=1;i<=m;i++){
        int a, b, c;
        cin >> a >> b >> c;
        edge[c].eb(mp(a, b));
    }
    int cnt = n+1;
    for(int i=1;i<=1000000;i++){
        vector<int> v;
        for(int j=0;j<sz(edge[i]);j++){
            int a = edge[i][j].fi, b = edge[i][j].se;
            merge(a, b);
            v.eb(a), v.eb(b);
        }
        sort(all(v));
        v.erase(unique(all(v)), v.end());
        for(int i=0;i<sz(v);i++){
            int u = v[i];
            if(root(u) == u) idx[u] = cnt++;
        }
        for(int i=0;i<sz(v);i++){
            int u = v[i];
            g[u].eb(mp(idx[root(u)], 1));
            g[idx[root(u)]].eb(mp(u, 0));
        }
        for(int i=0;i<sz(v);i++) p[v[i]] = v[i];
    }
    deque<pii> dq;
    memset(dp, -1, sizeof(dp));
    dp[1] = 0;
    dq.push_front(mp(dp[1], 1));
    while(!dq.empty()){
        int u = dq.front().se;
        int val = dq.front().fi;
        dq.pop_front();
        if(val != dp[u]) continue;
        for(int i=0;i<sz(g[u]);i++){
            int v = g[u][i].fi, w = g[u][i].se;
            if(dp[v] == -1 || dp[v] > dp[u] + w){
                dp[v] = dp[u] + w;
                if(w == 0) dq.push_front(mp(dp[v], v));
                else dq.push_back(mp(dp[v], v));
            }
        }
    }
    printf("%d\n", dp[n]);
}