n,k = gets.split.map(&:to_i)
point = Array.new(n).map{Array.new(2,-1)}
side = Array.new(n).map{Array.new(2,0)}
(n-1).times{|i| side[i] = gets.split.map(&:to_i)}
point[0][0] = 0
point[0][1] = -1
inst = [0]
check = Array.new(n,0)
check[0] = 1
count = 0
count_p = 1
while point[n-1][0] == -1
  count += 1
  inst2 = []
  for i in 0..n-2
    for j in 0..inst.length-1
      if (inst[j] == side[i][0]-1 && check[side[i][1]-1] == 0)
        point[count_p][0] = count
        point[count_p][1] = inst[j]
        check[side[i][1]-1] = 1
        inst2.push(side[i][1]-1)
        count_p += 1
      end
      if (inst[j] == side[i][1]-1 && check[side[i][0]-1] == 0)
        point[count_p][0] = count
        point[count_p][1] = inst[j]
        check[side[i][0]-1] = 1
        inst2.push(side[i][0]-1)
        count_p += 1
      end
    end
  end
  inst = Marshal.load(Marshal.dump(inst2))
end
