import heapq


def dijkstra():
    dp[sup_v][0] = 1
    q = [(0, sup_v, 0)] # q = [(startからの距離, 現在地)]
    while q:
        d, v1, v2 = heapq.heappop(q)
        if dp[v1][v2] < d:
            continue
        for nxt_v1, nxt_v2, cost in graph[v1][v2]:
            if dp[v1][v2] + cost < dp[nxt_v1][nxt_v2]:
                dp[nxt_v1][nxt_v2] = dp[v1][v2] + cost
                heapq.heappush(q, (dp[nxt_v1][nxt_v2], nxt_v1, nxt_v2))


n, m = map(int, input().split())
info = [list(map(int, input().split())) for i in range(m)]
INF = 10 ** 18


sup_v = 10 ** 6
graph = {sup_v: {}}
dp = {sup_v: {}}
for a, b, c in info:
    a -= 1
    b -= 1
    if c not in graph:
        graph[c] = {}
        dp[c] = {}
    if a not in graph[c]:
        graph[c][a] = []
        graph[c][a].append((sup_v, a, 1))
        dp[c][a] = INF
    if b not in graph[c]:
        graph[c][b] = []
        graph[c][b].append((sup_v, b, 1))
        dp[c][b] = INF
    if a not in graph[sup_v]:
        graph[sup_v][a] = []
        dp[sup_v][a] = INF
    if b not in graph[sup_v]:
        graph[sup_v][b] = []
        dp[sup_v][b] = INF

    graph[c][a].append((c, b, 0))
    graph[c][b].append((c, a, 0))
    graph[sup_v][a].append((c, b, 0))
    graph[sup_v][b].append((c, a, 0))
    

dijkstra()
ans = INF
for i in dp:
    for j in dp[i]:
        if j == n - 1:
            ans = min(dp[i][j], ans)            
print(ans)

### FIXED CODE ###

from collections import deque
import sys
input = sys.stdin.buffer.readline


def bfs():
    dp[sup_v][0] = 1
    q1 = deque([sup_v])
    q2 = deque([0])
    while q1:
        v1 = q1.popleft()
        v2 = q2.popleft()
        for tmp in graph[v1 * OFFSET + v2]:
            nxt_v1, nxt_v2 = divmod(tmp, OFFSET)
            if nxt_v1 == -1:
                cost = 1
            else:
                cost = 0
            if nxt_v1 not in dp:
                dp[nxt_v1] = {}
            if nxt_v2 not in dp[nxt_v1]:
                dp[nxt_v1][nxt_v2] = dp[v1][v2] + cost
                if cost == 0:
                    q1.appendleft(nxt_v1)
                    q2.appendleft(nxt_v2)
                else:
                    q1.append(nxt_v1)
                    q2.append(nxt_v2)
            elif dp[v1][v2] + cost < dp[nxt_v1][nxt_v2]:
                dp[nxt_v1][nxt_v2] = dp[v1][v2] + cost
                if cost == 0:
                    q1.appendleft(nxt_v1)
                    q2.appendleft(nxt_v2)
                else:
                    q1.append(nxt_v1)
                    q2.append(nxt_v2)
    
n, m = map(int, input().split())
INF = 10 ** 18
OFFSET = 10 ** 6


sup_v = -1
graph = {sup_v * OFFSET: []}
for _ in range(m):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    if c * OFFSET + a not in graph:
        graph[c * OFFSET + a] = []
        graph[c * OFFSET + a].append(sup_v * OFFSET + a)
    if c * OFFSET + b not in graph:
        graph[c * OFFSET + b] = []
        graph[c * OFFSET + b].append(sup_v * OFFSET + b)
    if sup_v * OFFSET + a not in graph:
        graph[sup_v * OFFSET + a] = []
    if sup_v * OFFSET + b not in graph:
        graph[sup_v * OFFSET + b] = []

    graph[c * OFFSET + a].append(c * OFFSET + b)
    graph[c * OFFSET + b].append(c * OFFSET + a)
    graph[sup_v * OFFSET + a].append(c * OFFSET + b)
    graph[sup_v * OFFSET + b].append(c * OFFSET + a)


dp = {sup_v: {}}
bfs()
ans = INF
for i in dp:
    for j in dp[i]:
        if j == n - 1:
            ans = min(dp[i][j], ans)   
if ans == INF:
    print(-1)
else:
    print(ans)