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 asked = {}
local tasknum = 0
local done = 0
local tasklim = 200000
for i = 1, n do
  asked[i] = false
  len[i] = {}
  taskstate[i] = {}
  for company, group in pairs(stations[i]) do
    len[i][company] = -1
    taskstate[i][company] = false
  end
end
local function addtask(station, company)
  if not taskstate[station][company] then
    taskstate[station][company] = true
    tasknum = tasknum + 1
    local taskidx = tasknum % tasklim
    if taskidx == 0 then taskidx = tasklim end
    tasks[taskidx] = {station, company}
  end
end
for company, group in pairs(stations[1]) do
  len[1][company] = 1
  addtask(1, company)
end
local e,f = 0,0
while done < tasknum do
  done = done + 1
  local taskidx = done % tasklim
  if taskidx == 0 then taskidx = tasklim end
  local src_station, src_company = tasks[taskidx][1], tasks[taskidx][2]
  local src_group = stations[src_station][src_company]
  local srclen = len[src_station][src_company]
  taskstate[src_station][src_company] = false
  if not asked[src_station] then
    asked[src_station] = true
    for dst_company, _g in pairs(stations[src_station]) do
      if src_company ~= dst_company then
        local dstlen = len[src_station][dst_company]
        if dstlen == -1 or srclen + 1 < dstlen then
          len[src_station][dst_company] = srclen + 1
          addtask(src_station, dst_company)
        end
      end
    end
  end
  if 0 < #companies[src_company][src_group] then
    for i = 1, #companies[src_company][src_group] do
      local dst_station = companies[src_company][src_group][i]
      local dstlen = len[dst_station][src_company]
      if dstlen == -1 or srclen < dstlen then
        len[dst_station][src_company] = srclen
        addtask(dst_station, src_company)
      end
    end
    companies[src_company][src_group] = {}
  end
end
local ret = -1
local mmi = math.min
for company, group in pairs(stations[n]) do
  local dstlen = len[n][company]
  if 0 < dstlen then
    if ret == -1 then
      ret = dstlen
    else
      ret = mmi(ret, dstlen)
    end
  end
end
print(ret)


Measured memory_bytes: 124792