#include <bits/stdc++.h>
using i64 = long long;

int main() {
    int n, m;
    scanf("%d%d", &n, &m);

    std::vector<std::tuple<int, int, int>> es;
    std::vector<std::pair<int, int>> w;
    for (int i = 0; i < m; i++) {
        int p, q, c;
      	scanf("%d%d%d", &p, &q, &c);
        --p; --q;
        es.emplace_back(p, q, c);
        w.emplace_back(p, c);
        w.emplace_back(q, c);
    }

    std::sort(w.begin(), w.end());
    w.erase(std::unique(w.begin(), w.end()), w.end());

    auto get = [&](int v, int c) {
        return n + std::lower_bound(w.begin(), w.end(), std::make_pair(v, c)) - w.begin();
    };

    std::vector<std::vector<std::pair<int, int>>> g(w.size() + n);
    for (const auto &e : w) {
        int v = get(e.first, e.second);
        g[e.first].emplace_back(v, 1);
        g[v].emplace_back(e.first, 0);
    }
    for (const auto &e : es) {
        auto p = std::get<0>(e), q = std::get<1>(e), c = std::get<2>(e);
        int pv = get(p, c), qv = get(q, c);
        g[pv].emplace_back(qv, 0);
        g[qv].emplace_back(pv, 0);
    }

    constexpr int inf = std::numeric_limits<int>::max() / 2;
    std::vector<int> dist(g.size(), inf);
    std::deque<int> q { 0 };
    dist[0] = 0;
    while (!q.empty()) {
        int v = q.front();
        q.pop_front();
        for (auto to : g[v]) {
            if (dist[to.first] <= dist[v] + to.second) continue;
            dist[to.first] = dist[v] + to.second;
            if (to.second) q.push_back(to.first);
            else q.push_front(to.first);
        }
    }

	printf("%d\n", dist[n - 1] == inf ? -1 : dist[n - 1]);
  
    return 0;
}

#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;
}
