local n, m = io.read("*n", "*n")
local ps, qs, cs = {}, {}, {}
local companies = {}
local stations = {}
for i = 1, n do
  stations[i] = {}
end
for i = 1, m do
  local p, q, c = io.read("*n", "*n", "*n")
  if not companies[c] then companies[c] = {} end
  if stations[p][c] and stations[q][c] then
    if stations[p][c] ~= stations[q][c] then
      local ci_p, ci_q = stations[p][c], stations[q][c]
      if #companies[c][ci_p] < #companies[c][ci_q] then
        for j = 1, #companies[c][ci_p] do
          local st = companies[c][ci_p][j]
          stations[st][c] = ci_q
          table.insert(companies[c][ci_q], st)
        end
        companies[c][ci_p] = {}
      else
        for j = 1, #companies[c][ci_q] do
          local st = companies[c][ci_q][j]
          stations[st][c] = ci_p
          table.insert(companies[c][ci_p], st)
        end
        companies[c][ci_q] = {}
      end
    end
  elseif not stations[p][c] and not stations[q][c] then
    table.insert(companies[c], {p, q})
    stations[p][c] = #companies[c]
    stations[q][c] = #companies[c]
  elseif stations[p][c] then
    local idx = stations[p][c]
    stations[q][c] = idx
    table.insert(companies[c][idx], q)
  else
    local idx = stations[q][c]
    stations[p][c] = idx
    table.insert(companies[c][idx], p)
  end
end
local len = {}
local taskstate = {}
local tasks = {}
local tasknum = 0
local done = 0
local tasklim = 200000
local c_g_num = 0
local c_g_map = {}
local c_g_stations = {}
for company, groups in pairs(companies) do
  c_g_map[company] = {}
  for i = 1, #groups do
    c_g_num = c_g_num + 1
    c_g_map[company][i] = c_g_num
    len[c_g_num] = -1
    taskstate[c_g_num] = false
    c_g_stations[c_g_num] = groups[i]
  end
end
local function addtask(c_g_idx)
  if not taskstate[c_g_idx] then
    taskstate[c_g_idx] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = c_g_idx
  end
end
for company, group in pairs(stations[1]) do
  local cgidx = c_g_map[company][group]
  len[cgidx] = 1
  addtask(cgidx)
end
local e = 0
while done < tasknum do
  done = done + 1
  local taskidx = done % tasklim
  if taskidx == 0 then taskidx = tasklim end
  local src_cg = tasks[taskidx]
  local srclen = len[src_cg]
  taskstate[src_cg] = false
  local tmptbl = {}
  for i = 1, #c_g_stations[src_cg] do
    local station = c_g_stations[src_cg][i]
    for dst_company, dst_group in pairs(stations[station]) do
      e = e + 1
      local dstcg = c_g_map[dst_company][dst_group]
      local dstlen = len[dstcg]
      if dstlen == -1 or srclen + 1 < dstlen then
        len[dstcg] = srclen + 1
        table.insert(tmptbl, dstcg)
        -- addtask(dst_company, dst_group)
      end
    end
  end
  table.sort(tmptbl, function(a, b) return len[a] < len[b] end)
  for i = 1, #tmptbl do
    addtask(tmptbl[i])
  end
end
local ret = -1
local mmi = math.min
for company, group in pairs(stations[n]) do
  local dstlen = len[c_g_map[company][group]]
  if 0 < dstlen then
    if ret == -1 then
      ret = dstlen
    else
      ret = mmi(ret, dstlen)
    end
  end
end
print(os.clock())
print(ret)
